#wordpress

Use LESSCSS Client Side While Developing WordPress Theme

I write in LESSCSS because I find it’s nesting so much eaiser to read. While creating a theme, I never compile during, it’s too annoying, instead I have a on/off toggle for using .LESS file or .CSS file.

https://gist.github.com/davidsword/a3760b1ae2e00802c677ea2a66132f08

In the snippet I made the toggle WP_DEBUG, which should be on while you develop, but if you’re developing in a network it’s eaiser to use a global veriable from functions.php

Furthermore if you are using LESSCSS and are developing/maintaining a lot of WordPress themes, I highly suggest making a shortcut for LESSCSS in bash:

https://gist.github.com/davidsword/71e290a3afd296e841bca3f33099d5e2

So all you have to do is type less when you’re in the theme directory. If you’re not comfortable with terminal, I’ve written about a PHP LESSCSS Compiler.

Remove Select POST FORMATS from Child Theme

The documentation left this out, but if you want to remove some post formats from a WordPress child theme, the remove_theme_support( 'post-formats') does not allow you to pass an array of formats, it’s just a hard switch. However you can over-write the original add_theme_support('post-formats') with only formats you desire.

https://gist.github.com/davidsword/a9087efdd6525f5217ffeaab2154111c

WordPress ShortCode API – How to make a function echo and not break

According to my previous understanding and WordPress’s documentation:

Note that the function called by the shortcode should never produce output of any kind. Shortcode functions should return the text that is to be used to replace the shortcode. Producing the output directly will lead to unexpected results

So, we’re limtied, shortcode functions must return, not echo. So:

https://gist.github.com/davidsword/394108430a76c36aa9cefb6c809dc187

Has been my understanding for years. Unless…

https://gist.github.com/davidsword/1a590c2a11bb55531ddfc98eb83afee3

Yeah. Yeah. That’s right! You can use output buffering to collect your echo’ed HTML from printing, and return it instead.

This is good to use when a shortcode has dozens of lines some multi-language script. For example I’m working on a custom Google Map with Javascript, it’s 200 lines, called to via [map] and has lots of PHP, HTML, some CSS, and WordPress conditional statements weaved into it. It’s a nightmare putting that much Javascript and multi-language markup into a PHP string, escaping quotes alone is a pile of trouble. What’s worse is the syntax highlighting from the IDE is lost, so any bug fixes or changes, you loose a great assistant. I know there’s other solutions, I could build a class, or I could do a file_get_contents() and reduce my PHP to being at the top of the script, or similar, but when the scripts not too long, it’s just so nice to have raw HTML inside the function, instead of dealing with everything else.


Update: Another great use for this is if you want to include() a local file’s echo‘ed results into a variable. If you use get_file_contents() you’ll receive the unexecuted code, storing executed code in an object allows you to return it into a variable. For the following example /file.php is just a simple file with echo "Rock the boat";

https://gist.github.com/davidsword/29aa612b73266c72b79e41bddbe01174

With the above, $files_output will be Rock the boat. Neat eh!

404’s Are Server, Not WordPress’s 404

Frustrating problem I’ve had happen more than once. Your WordPress site returns a 404 (which you may be relying on), but it shows a HTML (server generated) 404 page, not a WordPress 404 page.

Solution for apache is simple: let the server know to revert to index.php when the header is 404, set the following in .htaccess

https://gist.github.com/davidsword/07e6d391aad5a8f08580bda29d087dd7

Voilà

WordPress Get Post IDs of Navigation Menu

Always a pain, finding the actual POST TYPE object of a Navigation Menu. This is the most straight forward solution I found to get the relation array.

https://gist.github.com/davidsword/dd8c37ee312e1658960c583ea618eec1

Which will return

https://gist.github.com/davidsword/ffa6e3644fd0079fb69a94d1841c9276

WordPress Troubleshooting 101

I’m a semi-regular contributor on WordPress Support Forums. & as you develop and assist more, you realize that theres a basic troubleshooting routine to solve almost all vague/inexplicable problems – you typically do the same things. These problems include;

* /wp-admin/ not loading

  • Unable to drag and drop in dashboard
  • White screen instead of site
  • Unable to use Visual tab

Basically anything not normal or anything not working properly (that is not directly related to installing or using a new plugin. To get your WordPress site back online, here’s a breakdown of how to troubleshoot to find the issue:

1. Visit your error log to reveal PHP errors

  1. Turn wp_DEBUG on to reveal MySQL + PHP errors on your WordPress site
  2. Disable all plugins one-by-one to find the culprit
  3. Disable your theme, revert to Twenty-x
  4. Upload WordPress to fix any permissions or file errors (except /wp-content/ and wp-config.php)
  5. Comment out any .htaccess firewalls or non-wordpress rewrites

If any of these techniques work, refer to basic Google’ing on how to resolve the issue. Open source software controlling 25% of the web, the odds are in your favour that someone experienced it too and wrote about it.

For overly basic example; if you discover plugin FooBar was not allowing drag and drop to work in the backend, search ”Wordpress FooBar drag and drop not working” – and you should find your answer.

Doing the troubleshooting in the order should eliminate any ”I know it’s my theme, but I don’t know what’s not working” issues, your problem will likely spit out in PHP errors.

Add or Remove WWW in WordPress

www is technically a sub-domain – but it usually shares the same /httpdocs/ or /public_html/ or /www/ folder on hosts as non-www, it also is configurable in many different places and varies from host-to-host, so it gets confusing at times.

As such, this question comes up daily on the WordPress Support Forums and being that I’m tired of writing personalized responses to them – here’s a quick and easy break down:

Site Address and WordPress Address

When you’re in Dashboard > Settings > General ensure you’re changing both the “WordPress Address (URL)” and “Site Address (URL)” to include or exclude your www.

If you’ve locked yourself out of /wp-admin/ because of a www change and are seeing a white screen or experiencing a redirect loop: head to your database via phpMyAdmin, look for *_options and change the siteurl and home values to have or remove www.

If you have a WordPress Network / Multisite setup – there’s a few more steps for this:

* in wp-config.php make sure the DOMAIN_CURRENT_SITE is set to your www preference
in _blogs and *_site make sure everything’s set to your www preference
in all _*_options make sure home and site_url are also at your www preference

Check Your Web Host (control panel, plesk)

Changing www in WordPress isn’t always enough, sometimes your server needs adjustments. Any of the above hosting managers (often in the “domains” section) will have a question about your domain if you “want www prefix” to prepend to your domain. Select what you’d like!

You can see if www is enabled on your current domain’s server/host/setup by going to your WordPress’s readme.html file (which is an independent file that doesn’t have anything to do with PHP, database, or your entered url’s) Simply visit via your WordPress install location (example: http://WWW.yoursite.com/wordpress/readme.html)

Force www Preference with .htaccess

If it’s just being a pain, or perhaps your site is running www or non-www fine but the opposite is still rendering, make a redirect with .htaccess to immediately redirect users permanently (Search engines will respect this as they’re 301’s)

https://gist.github.com/davidsword/911630c964456fca18a0fcdb3b0ba0b2

The three areas above, used independently or together (depending on your setup and host) should resolve your www headache.