This website uses cookies to allow us to see how the site is used. If you continue to use this site, we assume that you are okay with this. If you want to use the sites without cookies, please see our privacy policy.

Find Pages using given WordPress shortcode

In WordPress it is quite easy to find all Pages or Post using given shortcode, below is a function that will do this, one important thing to keep in mind is that the shortcode you will be looking for needs to be registered (using add_shortcode) before using this function, otherwise the function will return NULL.

What i find cool about the has_shortcode() function is that it will find shortcodes regardless of how they are being used, with or without attributes.

function pages_with_shortcode($shortcode, $args = array()) {
    if(!shortcode_exists($shortcode)) {
        // shortcode was not registered (yet?)
        return null;

    // replace get_pages with get_posts
    // if you want to search in posts
    $pages = get_pages($args);
    $list = array();

    foreach($pages as $page) {
        if(has_shortcode($page->post_content, $shortcode)) {
            $list[] = $page;

    return $list;

// quick usage guide
// adding filter with low priority
add_filter("init", "_my_init_test", 0, 1000);
function _my_init_test() {
    foreach(pages_with_shortcode("my_super_shortcode") as $p) {
        echo $p->post_title." <br/>";

Properly enqueuing script and styles in WordPress

Often when developing a site, plugin or theme you will want to enqueue your custom script or style, it is worth to learn how to do this right in order to avoid conflicts with third party plugins and decrease number of HTTP requests (for example avoid loading jQuery twice).

Without any further additions below is code that will load custom script and style in the frontend

add_filter("init", "stc_init");
function stc_init() {
    // first step is to register your custom scripts and styles, the scripts will be loaded
    // plugins_url() as used as below will work only when scripts are in the same directory
    // as *this* php file
    wp_register_script("stc-frontend-js", plugins_url("stc-frontend.js", __FILE__), array("jquery"));
    wp_register_style("stc-frontend-css", plugins_url("stc-frontend.css", __FILE__));

    // now we can localize the script (if needed)
    wp_localize_script("stc-frontend-js", "stc_frontend_js_lang", array(
        "hello_world" => __("Hello World!", "my-locale")

add_action("wp_enqueue_scripts", "stc_enqueue_scripts");
function stc_enqueue_scripts() {
    // the two lines below will load in &lt;head&gt; stc-frontend-js, stc-frontend-css
    // and jquery (since stc-frontend-js has dependency on jquery,
    // additionally stc_frontend_js_lang variable will be printend in page
    // html code ... so we are all set to use the scripts

Couple of things to consider

  • In order to get this to work you will need two files stc-frontend.js and stc-frontend.css in the same directory as the PHP code will be.
  • The scripts and styles will be loaded in <head> section, so in order to load them wp_enqueue_script and wp_enqueue_style functions have to be executed before head section is printend. Second possibility is to pass fourth (file version) and fifth arguments to wp_register_script, if you set the fifth argument to true then scripts will be loaded in footer instead.
  • This code is for loading resources in the frontend only if you want to do the same in wp-admin, then add following line at the end
    add_action("admin_enqueue_scripts", "stc_enqueue_scripts");

The complete source code for this snippet you can get from here.