WordPress.org

Ready to get started?Download WordPress

Forums

detect shortcode before plugin execution (6 posts)

  1. paulsson
    Member
    Posted 3 years ago #

    Hey all,

    I'm pretty new to WordPress, but have been programming PHP, Python, Java for years so I am comfortable with code. I have read the plugin API and about the action and filter hooks but I can't find anyway to limit the execution of a plugin by detecting if a shortcode is present or not.

    The plugin I am using is the Shiba Gallery plugin (http://shibashake.com/wordpress-theme/super-wordpress-gallery-plugin) which seems lighter weight than NextGen Gallery but provides a lot more functionality than the builtin WordPress Gallery. This plugin overrides the default [gallery] shortcode to add more functionality. This plugin is being initialized on the wordpress init action hook which then enqueues a bunch scripts and css files. The problem is that it queues up and loads all these javascript and css files on pages that don't contain the [gallery] shortcode.

    Does anybody have any recommendations on how to have the plugin halt or not execute unless the shortcode is present on the current page? I would like to make this improvement and submit the changes back to the author.

    If I need to clarify anything or post code please let me know.

    Thanks,
    Erik

  2. Xephan
    Member
    Posted 3 years ago #

  3. paulsson
    Member
    Posted 3 years ago #

    Xephan,

    This looks exactly like what I need. I will let you know how it works out.

    Thanks,
    Erik

  4. Mark / t31os
    Moderator
    Posted 3 years ago #

    Pretty much how i'd do it, good approach.. :)

  5. paulsson
    Member
    Posted 3 years ago #

    Xephan and Mark,

    This approach works out great if I have the shortcode directly as part of my page (not in a template). Is there a way I can test if the shortcode gets executed by a page template?

    Say I have a template "product.php" that has a common layout for all my products including the [gallery] shortcode. Then when I create a new product page in the WordPress Admin I choose this template and I just enter a few "custom fields" for the page like: gallery_id, product_name, product_description, etc. Maybe there is a much better way to do something like this.... remember I'm a WordPress newbie.

    My product.php template then has the following code (snippet) in it:

    $product_info = get_post_custom();
    ....
    <?php echo do_shortcode('[gallery id="' . $product_info["gallery_id"][0] . '" type="jqgalleria"]'); ?>
    ....

    Is there a way I can test for this?

    Thanks for all the help so far!

    Erik

  6. paulsson
    Member
    Posted 3 years ago #

    Ok, I figure I don't have to put the shortcode in my template now. I can put it as the only thing in the page content and then print out the page content at the right spot in my template by using "the_content()" inside the loop and still pull all the other info for the product from "custom fields" to use in the template.

    Thanks again,
    Erik

Topic Closed

This topic has been closed to new replies.

About this Topic