Shiba

Adventures in WordPress

  • Home
  • Dog
  • Art
  • Contact
  • WordPress Articles
    • WP Plugins
    • WP Programming
    • WP Admin Panels
    • WP Theme Design
    • WP How-To
    • WP Theme Images
You are here: Home / WordPress Programming / Find Posts Dialog Box

Find Posts Dialog Box

by ShibaShake 10 Comments

The Find Posts dialog box can be a very useful feature to add to your plugin. For example, when we attach images to posts (from the Media >> Library menu) we use the Find Posts dialog box to search for our target objects.

Find Posts dialog box while attaching images to posts.
Find Posts dialog box while attaching images to posts.

In the dialog box above, we can search for post and page objects.

Custom post types also get automatically added. Our example Find Posts dialog box above allows us to search for gallery and background objects, both of which are custom post types.

Searching for all post objects with the string - test.
Searching for all post objects with the string - test.

Here we consider how to add a Find Posts dialog box to our plugin.

1. find_posts_div

We can add the Find Posts dialog box by using the find_posts_div WordPress function.

?>
<form name="plugin_form" id="plugin_form" method="post" action="">
        <?php wp_nonce_field('plugin_nonce'); ?> 
        // Other form elements and code ...
 	<?php find_posts_div(); ?>
</form>
<?php

Note – we want to include the find_posts_div function call within a HTML form, so that we may capture and further process its results.

2. Attach Find Posts dialog box to a button or link

After we add the find_posts_div command, we will not see anything new because it only shows up as a pop-up box that is tied to an event. For example, we may only want the dialog box to appear when we click on a button or a link.

We attach our dialog box to a link or button by using the Javascript onclick event.

<a onclick="findPosts.open('action','find_posts');return false;" href="#">
<?php esc_attr_e('Example Post Search'); ?>
</a>

The Javascript function findPosts.open will open our Find Posts dialog box and handle user search and selection. Once the user Selects a post object, our form will be automatically submitted. The results of the dialog box will be passed along with other elements of our form.

The findPosts.open function also allows us to pass in an additional form element if we so choose. In the example above, we pass in a form element called action with the value find_posts.

This form element will only get added when we submit the form through our Find Posts dialog box. Therefore, it is a good way to identify whether the form is submitted as a result of a search, or as a result of some other operation.

3. Add relevant scripts

When we click on the link however, nothing happens.

For our Find Posts dialog box to appear, we also need to add in the styles and scripts that it relies on.

	add_action("admin_print_styles", 'plugin_admin_styles' );
	add_action("admin_print_scripts", 'plugin_admin_scripts' );
	function plugin_admin_styles() {
		wp_enqueue_style('thickbox'); // needed for find posts div
	}
	
	function plugin_admin_scripts() {
		wp_enqueue_script('thickbox'); // needed for find posts div
		wp_enqueue_script('media');
		wp_enqueue_script('wp-ajax-response');
	}

The pop-up box is rendered using thickbox, so we need to add the thickbox style and script to our plugin page.

In addition, the Find Posts dialog box relies on the media and wp-ajax-response Javascript libraries.

Note – The general hooks admin_print_styles and admin_print_scripts will add the styles and scripts to ALL admin pages. Instead, it is more efficient to only add them to our specific plugin dialog box page.

To do this we can use the admin_print_scripts-{$plugin_page} and admin_print_scripts-{$plugin_page} hooks.

4. Get dialog box results

After we add the relevant scripts, our Find Posts dialog box will appear when we click on our Example Post Search link.

Once we choose the post that we want and click on the Select button, the results of our dialog box will be submitted with our form.

In particular, the following elements will be added to our form array –

$_POST['action'] = 'find_posts'$_POST['found_post_id'] = post object selected

The action form argument was added from the findPosts.open function call inputs. The found_post_id form argument is automatically added when the user selects a post object in our Find Posts dialog box.

5. We are done!

By using the find_posts_div native WordPress function, we can easily add the ability to search for posts without having to write any of the search code ourselves. Using such native functions will also give our plugin a more standard look, and allow us to benefit from new WordPress core features, such as custom post types.

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Comments

  1. Parham says

    March 7, 2014 at 12:28 am

    Hey, Is it possible to limit the results to some specific post types?

    Reply
  2. Brennan says

    August 16, 2013 at 6:53 am

    Hey,

    I wanted to implement this on my plugin. It worked so far, but there is no selection (as in your screenshots) for posts, pages, etc. How can I add this? Or better: how can I filter to show only pages?

    Thanks for your help!

    Reply
  3. Luca says

    January 10, 2013 at 2:16 am

    Hi there, great article!

    I followed your instructions and the dialog box opens correctly, but for some reason there are no radio buttons, and the search does not work, as it always returns a “-1”. I’m on WordPress 3.5.

    Do you have any clue about why does this happen? Thanks!

    Reply
    • ShibaShake says

      January 10, 2013 at 11:32 am

      I have working examples of the dialog box in several of my plugins. Perhaps looking at an example will help. The easiest one to look at is probably in the Shiba Comments plugin.

      Reply
      • Luca says

        January 11, 2013 at 12:08 am

        Thanks Shiba, for some reason the dialog has started working by itself. Thanks anyway and KUTGW. šŸ˜‰

  4. camu says

    January 3, 2013 at 6:58 pm

    Hi there, is there any chance this can be adapted to WP 3.5. I was following your step-by-step instructions, but for some reason it’s not working on my blog šŸ™

    Reply
    • ShibaShake says

      January 5, 2013 at 9:35 am

      What errors are you getting?

      I use this dialog box in several of my plugins, and have not encountered any errors in 3.5. There is an example of how I use this in the Shiba Gravatar plugin, which I have already updated for 3.5.

      Reply
  5. Andrea says

    November 9, 2010 at 1:21 pm

    Thank you. i will try…
    Ciao and “bravo” for the very best code

    Andrea – ITALY

    Reply
  6. Andrea says

    November 6, 2010 at 10:20 am

    It is possible to view a complete listing of the code, for viewing the page or the post as in the first image of the post.
    Thank you

    Reply
    • ShibaShake says

      November 8, 2010 at 1:57 pm

      I use this dialog box in several of my plugins, so feel free to look through their code for examples. The easiest one to look at is probably the Shiba Gravatar plugin. It is simpler than most of the others.

      Reply

Recent Posts

  • Screenshot of an example article in code view of a modified Gutenberg editor.How to Harness the Power of WordPress Gutenberg Blocks and Combine It with Legacy Free-Form Text
  • Screenshot of the Success, WordPress has been installed page.Migrating Your WordPress Website to Amazon EC2 (AWS)
  • Screenshot of WinSCP for creating a SFTP configuration.How to Set-Up SFTP on Amazon EC2 (AWS)
  • WordPress Gutenberg code view screenshot of this article.How to Prevent Gutenberg Autop from Messing Up Your Code, Shortcodes, and Scripts
  • Screenshot of the Success, WordPress has been installed page.How to Create a WordPress Website on Amazon EC2 (AWS)

Recent Comments

  • Screenshot of the Success, WordPress has been installed page.How to Create a WordPress Website on Amazon EC2 (AWS) (1)
    • Erik
      - Great article. All worked great except for this step:apt install php-mysqlChanging to this fixed it:apt install ...
  • Add Custom Taxonomy Tags to Your WordPress Permalinks (125)
    • Anthony
      - Where does this code go? Like, what exact .php file please?
  • Screenshot of an example article in code view of a modified Gutenberg editor.How to Harness the Power of WordPress Gutenberg Blocks and Combine It with Legacy Free-Form Text (1)
    • tom
      - hi,my experience was like the same, but for me as theme developer the "lazy blocks"(https://wordpress.org/plugins/lazy-blocks/) ...
  • WordPress Custom Taxonomy Input Panels (106)
    • Phil T
      - This is unnecessarily confusing. Why declare a variable with the same name as your taxonomy? Why did you choose a taxonomy ...
  • Create Pop-up Windows in Your WordPress Blog with Thickbox (57)
    • Jim Camomile
      - I have used this tutorial several times and it is one of the few simple and effective ways to add popups with dynamic content, ...

Copyright © 2025 · Genesis Skins by ShibaShake · Terms of Service · Privacy Policy ·