1

I have a long-term client with a ton of custom shortcodes and functions for miscellaneous purposes we've accrued over the years and I decided to re-home them into a custom plugin so I didn't have to migrate them from theme to theme. But for whatever reason, I can't get the shortcode to render through the plugin.

My current plugin directory looks like (filler names):

  • plugin (prime directory)
    • primary-plugin-file.php
    • admin (directory)
      • css (directory)
        • admin-styles.css
      • admin-page.php
    • shortcodes (directory)
      • shortcode-1.php

(the point of the admin page is the "Read Me" and documentation - just getting that out of the way)

While I know I could just throw all of their custom functions into the primary-plugin-file.php file, I wanted to be a little more organized. I got the admin page set up just fine and I copied in one of their shortcodes (that I knew worked) and pasted it into my shortcode-1.php file (changing the shortcode name, of course). Unfortunately, all it does is display the shortcode itself (so when I used a shortcode block, the output is [shortcode]).

I've created tons of shortcodes for functions.php but for whatever reason, I can't get it to correctly call through the plugin.

Troubleshooting

  • I tested it by pasting the shortcode into the primary-plugin-file.php file but it still didn't work
  • I know I'm calling the file correctly because I intentionally broke it and it successfully broke the site
  • I pasted a basic shortcode test I found somewhere into the shortcode-1.php file and it still only shows up as [test_shortcode]
  • I tried some miscellaneous re-arranging by studying other plugin files (like I said, I'm a bit new at creating multi-file plugins), such as keeping the function in my shortcode-1.php files but putting add_shortcode('myCustomShortcode', 'this_particular_function'); under my require_once in my primary-plugin-file.php
  • Tons of Googling that primarily results in "Plugins that will create shortcodes for you!" articles

The Code

Here is my primary primary-plugin-file.php:

// variable path
if ( ! defined( 'MY_PLUGIN_DIR' ) ) {
    define( 'MY_PLUGIN_DIR', plugin_dir_path( __FILE__ ) );
}

// Add Admin Page
require_once( MY_PLUGIN_DIR . 'admin/admin-page.php' );


// Add Shortcodes
require_once( MY_PLUGIN_DIR . 'shortcodes/shortcode-1.php' );

Here is my shortcode-1.php file:

add_shortcode('myCustomShortcode', 'this_particular_function');

function this_particular_function($atts) {
    $return = ''; 
    ob_start();
    $args = array(
        'post_type'=> 'post-type',
        'category_name' => 'cat',
        'post_status' => 'publish',
        'posts_per_page' => 1,
    );

    $custom_query = new WP_Query( $args );
    if($custom_query->have_posts() ):
        while ( $custom_query->have_posts() ):
            $custom_query->the_post();
            $thingID = get_the_ID();
            
            
            ?>
            
        <div class="home-current-thing-wrapper"> 
        
        Display the thing
        
        </div>
        
        <?php
        endwhile;
    endif;
    $return = ob_get_clean();
    
    return $return;
}

I stripped a bunch of it out but left enough for you to get the idea. I'm really not sure why the shortcode wouldn't be rendering, but it's obvious I'm doing something wrong. I'd appreciate any advice here. Thanks!

(Not sure if it's important or not, but my Admin page works just fine)

1 Answer 1

1

add_shortcode('myCustomShortcode', 'this_particular_function');

As soon as I looked at my code this morning I realized I was trying to pass the function name ([this_particular_function]) as the shortcode, not the shortcode name ([myCustomShortcode]).

I overlooked this SO many times yesterday trying to look for some complex answer! I am smacking my head on the desk. Everything works great now, my bad.

The answer: Ockham's razor.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.