Using a menu walker add a custom item at the end of the menu’s items

You don’t need a walker in this case. A filter called wp_nav_menu_items is available. It allows you to edit the list items of a menu. Just append your own list item with search field. add_filter( ‘wp_nav_menu_items’, ‘add_search_to_nav’, 10, 2 ); function add_search_to_nav( $items, $args ) { $items .= ‘<li>SEARCH</li>’; return $items; } Note: if you … Read more

WP_Query leaking absurd amounts of memory

Excellent responses on WP Hackers: http://lists.automattic.com/pipermail/wp-hackers/2012-June/043213.html What you’re doing with that query, is loading EVERY matching post into memory, including the full post contents. As you can imagine, this is probably quite a lot of items. You can pass ‘fields’ => ‘ids’ into WP_Query to simply return a list of matching post_ids instead, which should … Read more

Is it bad practice to create own table for a plugin?

I rarely disagree with otherwise knowledgeable users, but in this case I can’t help it. In my opinion calling the usage of non-core database tables bad practice per se is just simply wrong. The choice of whether to go with core tables or add your own depends on several factors. A query’s execution time depends … Read more

Nested custom post types with permalinks

If you want to keep ‘authors’ as the base slug in the permalinks, i.e. example.com/authors/stephen-king/ for the ‘authors’ CPT, example.com/authors/stephen-king/the-shining/ for the ‘books’ CPT and example.com/authors/stephen-king/the-shining/chapter-3/ for the ‘chapters’ CPT, WordPress will think pretty much everything is an ‘authors’ post or a hierarchical child of an ‘authors’ post and, since that is not the case, … Read more

Run function AFTER theme options are saved?

Use the filter update_option_{$option}. It runs after a successful saving. $option is the name of your option, and you get the old and the new value as parameters. From wp-includes/option.php: do_action( “update_option_{$option}”, $oldvalue, $_newvalue ); Use it like this for an option wpse_themesettings: add_action( ‘update_option_wpse_themesettings’, ‘wpse_check_settings’, 10, 2 ); function wpse_check_settings( $old_value, $new_value ) { … Read more

How to connect different CPTs together?

As a programmer, I would start writing my own code to connect my post types. It’s not a quick way, it’s not easy, but it’s a lot of fun. The post types We start by creating two simple post types, Author and Books: add_action(‘init’, ‘p2p2_register_author’); add_action(‘init’, ‘p2p2_register_book’); function p2p2_register_author(){ $labels = array( ‘name’ => ‘Author’, … Read more

Parent child relationship in custom post type

In your register_post_type call, make sure you have these arguments: register_post_type( ‘my_post_type’, array( ‘hierarchical’ => true, ‘public’ => true, ‘rewrite’ => array( ‘slug’ => ‘my_post_type’, ‘with_front’ => false, ), ‘supports’ => array( ‘page-attributes’ /* This will show the post parent field */, ‘title’, ‘editor’, ‘something-else’, ), // Other arguments ) ); Make sure your permalinks … Read more