WordPress.org

Ready to get started?Download WordPress

Forums

Override shortcode for mobile theme? (7 posts)

  1. miraclemaker
    Member
    Posted 2 years ago #

    Hi guys. I have a site that has an alternative theme for mobile devices.

    There are several shortcodes that the site uses to embed youtube videos, Google Maps etc. When the mobile theme is in use, I would like to override these shortcodes to simply display a link to the content, rather than embedding the content itself. Any tips on doing this?

    Many thanks!

  2. Rebecca Schaefer
    Member
    Posted 2 years ago #

    I would try using remove_shortcode in the mobile theme's functions.php.

    [silly and oversimplified] example

    function newGoogleVideoShortcode($atts) {
     return($atts['link']);
    }
    remove_shortcode('google_video');
    add_shortcode('google_video', 'newGoogleVideoShortcode');

    As long as it's in the mobile theme's functions file, it should only remove those codes when the mobile theme is in use.

  3. miraclemaker
    Member
    Posted 2 years ago #

    Thanks this worked great. I found I was able to override the shortcode just by using add_shortcode(), no need to remove it first.

    I had a problem with one plugin (Viper's Video Shortcodes) where this method didn't work. I got round it by using an alternative plugin.

  4. bitsyandkitty
    Member
    Posted 2 years ago #

    Hi, I have a similar problem and wondering if either of you can help.
    I have a mobile theme (carrington) that shows the shortcodes coding of the regular site, i'd simply like it hidden or ignored. in fact the site does ignore it but shows the code so how can i hide it?
    Please spell it out for a non-techie!
    many thank in advance.
    V. x

  5. miraclemaker
    Member
    Posted 2 years ago #

    Hi bitsyandkitty.

    All you need to do is register the shortcode you wish to hide in the functions.php of your mobile theme.

    Here's a very simple example:

    function my_shortcode_handler( $atts, $content=null, $code="" )
        {
            return '';
        }
    
        //override the 'testimonial' shortcode
        add_shortcode( 'testimonial', 'my_shortcode_handler' );
  6. bitsyandkitty
    Member
    Posted 2 years ago #

    Hi Miraclemaker- that is fab - thanks so much. a very handy piece of code for rest of 2012 no doubt! x

  7. kschackai
    Member
    Posted 2 years ago #

    I'm quite possibly being a moron, but I've tried miraclemaker's code above several times now, and get a fatal error every time. Are there some parameters for where this should go that I'm missing?

    Background: I'm trying desperately to mobilize a theme from Elegant Themes using their theme/plugin Handheld. The problem is that shortcodes on the main site are removed in the mobile version, which also removes the content inside them. I made a change to handheld.php which made the content visible, but now it's wrapped in visible shortcode text, which I need to get rid of.

    I tried the above code, edited to reference two of Elegant Themes' shortcodes ([dropcap] and [one_third]), but both times the plugin was deactivated with a fatal error.

    Example:

    function my_shortcode_handler( $atts, $content=null, $code="" )
    {
    return '';
    }

    //override the 'dropcap' shortcode
    add_shortcode( 'dropcap', 'my_shortcode_handler' );

    Any idea what I'm doing wrong?

Topic Closed

This topic has been closed to new replies.

About this Topic