WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] How come shortcode functions can't be pluggable?? (5 posts)

  1. chriscarvache
    Member
    Posted 1 year ago #

    I've noticed this...

    add_action ('my_custom_action', 'my_custom_action_function');
    
    if ( !function_exists('my_custom_action_function') ) {
        function my_custom_action_function() {
            echo 'This works';
        }
    }

    This makes an action function pluggable. But how come when you take the SAME approach to shortcodes it doesn't work and doesn't register the shortcode? Is this a bug or by design? If this is by design, then for what reason?

    Thanks!!

    ~C

  2. alchymyth
    The Sweeper & Moderator
    Posted 1 year ago #

    what is the exact code that is not working?

    why are you not using remove_shortcode() http://codex.wordpress.org/Function_Reference/remove_shortcode

  3. chriscarvache
    Member
    Posted 1 year ago #

    The code I was using looked like this...

    add_shortcode ('my_custom_shortcode', 'my_custom_shortcode_function');
    
    if ( !function_exists('my_custom_shortcode_function') ) {
        function my_custom_shortcode_function() {
            echo 'This works';
        }
    }

    If you declare a shortcode like this it won't work.

    I suppose I could use the remove_shortcode() function. I didn't even think of that. Thanks for good feedback and suggestion but, that doesn't answer my question.

    Why can actions be pluggable but shortcodes can't?

    It just seems simpler to me to override a function in a child theme rather than have to remove the shortcode and redeclare it. Although its only 2 more lines of code, its STILL 2 more lines of code. I'm just looking for the logic behind why it won't work if you declare the shortcode as mentioned.

  4. alchymyth
    The Sweeper & Moderator
    Posted 1 year ago #

    changing the order of the code seems to work:

    in the parent theme:

    if ( !function_exists('my_custom_shortcode_function') ) {
        function my_custom_shortcode_function() {
            return 'This works';
        }
    }
    add_shortcode ('my_custom_shortcode', 'my_custom_shortcode_function');

    in the child theme:

    function my_custom_shortcode_function() {
            return 'Dis wurkz';
        }

    overwrites the parent shortcode function.

  5. chriscarvache
    Member
    Posted 1 year ago #

    Okay cool. Looks good to me!

Topic Closed

This topic has been closed to new replies.

About this Topic