95

Okay, I've had two big projects where I've been in control of the server enough to namespace and rely on autoloading. First up. Autoloading is awesome. Not worrying about requires is a relatively good thing. Here's a loader I've been using on a few projects. Checks to make sure the class is in the current namespace first, then bails if not. From there it'...


77

I would start with this question: Is the functionality related to presentation of content, or with generation/management of content, or of the site, or of the user identity? If the functionality is not related specifically to presentation of content, then it is squarely within Plugin Territory. This list is long: Modifying core WP filters (wp_head content, ...


62

Visit the permalinks page (which will flush it) and check again. WordPress probably just needs to be nudged to recognize your addition to the hierarchy.


55

In backend there is global ajaxurl variable defined by WordPress itself. This variable is not created by WP in frontend. It means that if you want to use AJAX calls in frontend, then you have to define such variable by yourself. Good way to do this is to use wp_localize_script. Let's assume your AJAX calls are in my-ajax-script.js file, then add ...


53

An easy test where the code is best placed: write the code into the functions.php switch theme do you miss the functionality, is the blog not proper working or fragments of the old theme (e.g. shortcodes) are left? yes: put it into a plugin no: leave it in functions.php Examples: Write a shortcode. After switching the theme, the plain shortcodes are left ...


44

If you want to echo the translated string, then you will be using _e and when you just want to have the translated string, then you will be using __. Example: _e('this is a message', 'twentyfourteen'); is same as echo __('this is a message', 'twentyfourteen');


43

to use ajaxurl directly, in your plugin file add this: add_action('wp_head', 'myplugin_ajaxurl'); function myplugin_ajaxurl() { echo '<script type="text/javascript"> var ajaxurl = "' . admin_url('admin-ajax.php') . '"; </script>'; } you can then use the ajaxurl for ajax request.


39

Another option is getting the slug by post ID: $slug = get_post_field( 'post_name', $post_id ); Here is more info about get_post_field https://codex.wordpress.org/Function_Reference/get_post_field


39

Here is how you can create a .pot file for your theme with Poedit (free edition, version 1.6.10) on OS X. Best practise is to save language files in a folder named "languages" in your theme directory. If you haven't already, create it before you start. In Poedit: In the "File" menu select "New" Select the language that you used in your theme (probably ...


38

What you're looking for is wp_localize_script function. You use it like this when enqueing script wp_register_script( 'my-script', 'myscript_url' ); wp_enqueue_script( 'my-script' ); $translation_array = array( 'templateUrl' => get_stylesheet_directory_uri() ); //after wp_enqueue_script wp_localize_script( 'my-script', 'object_name', $translation_array )...


36

Since WordPress 3.0, you don't need a custom walker anymore! There is the walker_nav_menu_start_el filter, see https://developer.wordpress.org/reference/hooks/walker_nav_menu_start_el/ Example: function add_description_to_menu($item_output, $item, $depth, $args) { if (strlen($item->description) > 0 ) { // append description after link ...


36

As of WordPress 4.5 you can use: get_post_type_archive_link( 'post' ); This handles the logic of getting the correct URL regardless of whether posts show up on the homepage or in a specified page.


35

If you are within the search template i.e Search query is your main query. You should then be able to get search results from global $wp_query without running an additional query. global $wp_query; echo $wp_query->found_posts.' results found.'; Edit 1 If you have to get count out of search context. You can combine both techniques to get efficient ...


35

There are many themes and plugins using jQuery as loaded by WP core. Every time jQuery is updated, there is a risk of older themes and plugins breaking (because lots of them are still used but no longer updated). That's why you also see jquery-migrate.js in your source code. It's a script that catches old functions used by plugins/themes and makes sure that ...


33

Using @import in WordPress for adding custom css is no longer the best practice, yet you can do it with that method. the best practice is using the function wp_enqueue_style() in functions.php. Example: wp_enqueue_style ('theme-style', get_template_directory_uri().'/css/style.css'); wp_enqueue_style ('my-style', get_template_directory_uri().'/css/mystyle....


28

admin-ajax.php loads wp-load.php: /** Load WordPress Bootstrap */ require_once( dirname( dirname( __FILE__ ) ) . '/wp-load.php' ); wp-load.php loads wp-config.php, and there wp-settings.php is loaded. And here we find this: // Load the functions for the active theme, for both parent and child theme if applicable. if ( ! defined( 'WP_INSTALLING' ) || 'wp-...


27

Usually, you don’t need it. But … there is at least one edge case: If a theme file is a template part, and it is using global variables from the calling context (parent file), and register_globals is on, and it is just using these variables without any security check … … an attacker can call this file, set the missing variables with GET or POST and make ...


27

When declaring your custom post type using the register_post_type function, you have to add 'page-attributes' to the support field, like in the following example: register_post_type('myposttype', array( 'supports' => array('title', 'editor', 'page-attributes'), 'hierarchical' => false )); You'll need to add any other supported meta boxes as ...


27

You can do this with the WordPress tools, without POEdit. First, do an svn checkout of http://develop.svn.wordpress.org/trunk/: svn co http://develop.svn.wordpress.org/trunk/ wpdev Then, switch to the i18n tools directory in it: cd wpdev/tools/i18n/ Then just run the makepot.php over your theme's directory: php makepot.php wp-theme /path/to/your/theme ...


26

In short: index.php is a fallback template only, in case no appropriate template was found home.php is used for the blog (a listing of recent posts) front-page.php is used for the landing-page The universal index.php template The index.php template file is a fallback template. It is used as a last resort when no other more appropriate template is ...


26

For every enqueue action, there is a corresponding print_styles action: wp_enqueue_scripts → wp_print_styles admin_enqueue_scripts → admin_print_styles customize_controls_enqueue_scripts → customize_controls_print_styles Not for the login page. There is no login_print_styles action or function, so your stylesheets are thrown out on do_action( 'login_footer'...


26

You just need to add the styles directly to the page head. The best way to do this is to use the 'wp_head' action hook, assuming you are using a theme that has the hook. Like so: add_action('wp_head', 'my_custom_styles', 100); function my_custom_styles() { echo "<style>*{color: red}</style>"; } Check out the WP codex to learn more about ...


25

If you want display an image that is inserted into your content (a hotlinked image, for example), you must use a function like this (source): add in functions.php: function catch_that_image() { global $post, $posts; $first_img = ''; ob_start(); ob_end_clean(); $output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->...


25

In this case, 'themify' is the defined textdomain for the Theme, used to make the Theme translatable. (Codex reference: load_theme_textdomain()). Making a Theme translation-ready requires a few steps. Define the Theme's textdomain: load_theme_textdomain( 'themify', TEMPLATEPATH.'/languages' ); Define translatable strings in the template. This is done ...


25

You could check to see if the 'WooCommerce' class exists, then run the code that requires WooCommerce. <?php if ( class_exists( 'WooCommerce' ) ) { // code that requires WooCommerce } else { // you don't appear to have WooCommerce activated } ?>


23

To have the theme listed: style.css With at minimum this: /* Theme Name: Minimum Theme Description: Test Author: Test Version: 1.0 */ For the theme to be functional: index.php index.php must have a post loop, so this would be the bare minimum functional index.php <html> <head><?php wp_head(); ?></head> <body> <?php ...


23

It's not outdated Moving to 2.x or 3.x is too early, people still use IE8 which is only supported on 1.x


22

Well, you are using: get_post_meta(get_the_ID(), 'subtitle', TRUE); So, you are saying to Wordpress to get the meta value of the 'subtitle' field and that the returned value be in format of string. See get_post_meta() docu. To get all meta data of a post you should use get_post_custom() function instead. For example, if you are inside the loop: $custom = ...


21

This is not recommended in any WordPress style guide, and I think it is a bad coding style. Beginners are using this style, maybe because it feels more like HTML … Unfortunately, the default themes are using this style way too often, so some beginners might think it is part of a code style. One disadvantage of this style is comment handling. Look closely ...


20

I don't think there's an easy answer to this question, but I bet we could make a flow chart to help with the decision. Here's a rough outline of such a flow chart, which can and should be expanded. Comment with suggestions! Is this code to be hosted on a single-site installation of WordPress? Yes - Does the site's theme only change with major redesigns and ...


Only top voted, non community-wiki answers of a minimum length are eligible