96

You can extend the get_the_archive_title filter which I've mentioned in this answer add_filter( 'get_the_archive_title', function ($title) { if ( is_category() ) { $title = single_cat_title( '', false ); } elseif ( is_tag() ) { $title = single_tag_title( '', false ); } ...


87

get_template_directory_uri() will always return the URI of the current parent theme. To get the child theme URI instead, you need to use get_stylesheet_directory_uri(). You can find these in the documentation, along with a list of other useful functions for getting various theme directory locations. If you prefer to use a constant, then TEMPLATEPATH is ...


43

If you look at the source code of get_the_archive_title(), you will see that there is a filter supplied, called get_the_archive_title, through which you can filter the output from the function. You can use the following to change the output on a category page add_filter( 'get_the_archive_title', function ( $title ) { if( is_category() ) { $...


39

Checking to see if built in WordPress functions exist before calling them is for backward compatibility which IMHO is not needed. So if you see if ( function_exists( 'register_nav_menus' ) ) the theme author is supporting versions earlier than 3.0. You still sometimes see if ( function_exists( 'dynamic_sidebar' ) ) Why? I couldn't tell you because ...


38

Specifically editing the code you provided in the original question: add_filter( 'nav_menu_link_attributes', 'wpse121123_contact_menu_atts', 10, 3 ); function wpse121123_contact_menu_atts( $atts, $item, $args ) { // The ID of the target menu item $menu_target = 123; // inspect $item if ($item->ID == $menu_target) { $atts['data-toggle'] = '...


36

From the Codex ... Should the permastruct be prepended with the front base. (example: if your permalink structure is /blog/, then your links will be: false->/news/, true->/blog/news/). Defaults to true


35

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

Whenever you find a piece of code without clear installation instructions it is probably a plugin. The example you gave is a good one, because that is the most common case: add_action('template_redirect', 'remove_404_redirect', 1); function remove_404_redirect() { // do something } To use such a snippet, put it into a plugin: Create a new file, name it ...


34

Well the second bit of code is technically the correct way to do it. However, at the end of the add_settings_field() you can pass arguements. Please view the WordPress Add_Settings_Field function reference. This will help you in getting the best understanding of how the add_settings_field() function really works. Now, with that said, you could use a '...


33

The default response from admin-ajax.php is, die( '0' ); ...by adding your own wp_die() or exit() or die() after returning your desired content prevents the default response from admin-ajax.php being returned as well. It also generally means that your ajax call has succeeded. Ultimately, to answer your question, it's meant to work this way. What you ...


32

I would highly recommend Daniel's much simpler solution over the one currently selected as correct: $user = get_userdata( $user_id ); if ( $user === false ) { //user id does not exist } else { //user id exists }


31

The accepted answer works to remove the Category: prefix from category archive titles, but not other taxonomy or post types. To exclude other prefixes, there are two options: Rebuild the title for all the variants used in the original get_the_archive_title() function: // Return an alternate title, without prefix, for every type used in the ...


31

You can create new schedule times via cron_schedules: function my_cron_schedules($schedules){ if(!isset($schedules["5min"])){ $schedules["5min"] = array( 'interval' => 5*60, 'display' => __('Once every 5 minutes')); } if(!isset($schedules["30min"])){ $schedules["30min"] = array( 'interval'...


29

When your going through your loop add this: global $post; $post = get_post( $ID, OBJECT ); setup_postdata( $post ); //Do something wp_reset_postdata();


29

While I strongly advise against this, and it will not speed things up, your usage is incorrect. WordPress already caches these things in the object cache, you don't need to store the result and reuse, WP does that already. It's very likely your code is running slower as a result of this micro-optimisation, not faster! How To Use Globals When you try to ...


28

Use function single_term_title()


28

The error message shows that the class description_walker extends the Walker_Nav_Menu class and overrides the start_el() method, but the signature differs from the method definition in the parent class. As mentioned in comments, it would be best to contact the developer of your theme and ask for a corrected version of the theme. If the developer cannot be ...


27

As @cybmeta already pointed out, you are too early with your removal. So you have to defer the actual removal, for instance like so: add_action( 'after_setup_theme', 'wpdev_170663_remove_parent_theme_stuff', 0 ); function wpdev_170663_remove_parent_theme_stuff() { remove_action( 'after_setup_theme', 'et_pb_setup_theme' ); }


24

You can use the following instead, if (is_singular('post')) { //your code here... } Where by is_singular is the WordPress API conditional function for testing for the existence of a post type. You can also pass an array of post types if you wish. http://codex.wordpress.org/Function_Reference/is_singular


24

There's actually a filter that you can use, see abstract-wc-email.php, line 214: return apply_filters( 'woocommerce_email_recipient_' . $this->id, $this->recipient, $this->object ); you can put the following in your functions.php: add_filter( 'woocommerce_email_recipient_customer_completed_order', 'your_email_recipient_filter_function', 10, 2); ...


24

The code you have provided is incomplete. Not sure if that is the only code you are using to achieve what you want. So, besides first code block which you have provided, bellow I am adding all rest of the code which is required to show the new field on backend in 'Order Details' box and make it editable through custom fields. Please note, in your second code ...


22

Checkout - wp-includes/plugin.php#L585 plugin_dir_url() function internally uses plugins_url() to get the link to plugin directory. plugin_dir_url() This will return url of the plugin directory with a trailing slash at the end. So this can be easily used to link to the plugin directory. e.g - http://www.example.com/wp-content/plugins/foo/ plugins_url If no ...


22

Use a custom walker, extend the methods start_lvl() and end_lvl. Sample code, not tested: class WPSE_78121_Sublevel_Walker extends Walker_Nav_Menu { function start_lvl( &$output, $depth = 0, $args = array() ) { $indent = str_repeat("\t", $depth); $output .= "\n$indent<div class='sub-menu-wrap'><ul class='sub-menu'>\n"; ...


22

using wp_die(); at the end of AJAX function fixed the issue for me. e.g add_action( 'wp_ajax_my_ajax_function', 'my_ajax_function' ); function my_ajax_function(){ echo json_encode($myvar); wp_die(); }


22

esc_html() escapes a string so that it is not parsed as HTML. Characters like < are converted to &lt;, for example. This will look the same to the reader, but it means that if the value being output is <script> then it won't be interpreted by the browser as an actual script tag. Use this function whenever the value being output should not ...


21

Don't use global variables, as simple as that. Why not to use globals Because the use of globals makes it harder to maintain the software in the long term. A global can be declared anywhere in the code, or nowhere at all, therefor there is no place in which you can instinctivly look at to find some comment about what the global is used for While reading ...


20

The most comprehensive method is get_current_screen added in WordPress 3.1 $screen = get_current_screen(); returns WP_Screen Object ( [action] => [base] => post [id] => post [is_network] => [is_user] => [parent_base] => edit [parent_file] => edit.php [post_type] => post [taxonomy] => )


20

Updating the posts $all_posts = get_posts( 'posts_per_page' => -1, 'post_type' => 'post' ); foreach ( $all_posts as $single ) { wp_update_post( array( 'ID' => $single->ID, 'post_title' => to_title_case( $single->post_title ) // see function below )); } Converting a string to "Title Case" And, while not ...


20

You can also use $post_id = $_GET['post']; Or you can use a hook (probably better). function id_WPSE_114111() { global $post; $id = $post->ID; // do something } add_action( 'admin_notices', 'id_WPSE_114111' ); You will need to add a conditional since this will run on all admin pages, I recommend using get_current_screen(); For example to ...


19

Your question is a bit specific if you "only" want to automatically import some posts/pages. There are other ways to do this then using a XML export file. If you have text-only posts, then you should use LOAD DATA INFILE. At first you have to export your posts. global $wpdb, $wp_filesystem; $tables = array( 'posts' => array( 'posts', '...


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