• I’ve been using some custom shortcodes in my template. Thing is… I have to many and they are almost all identical. (over 40 times the same 4 lines of code with only 1 numerical change in the code)

    function t1_func( $atts ){
     return '<img class="alignnone" src="/Tekens%201.png" alt="" width="35" height="41" />';
    }
    add_shortcode( 't1', 't1_func' );

    Above is one current shortcode example. I use this same piece of code 9 times. Going from t1 to t9.

    I am now trying to make this code shorter and make it work for other attributes (b,t and k).

    I experimented with the following code:

    function m_func( $atts ) {
    	extract( shortcode_atts( array('b','t','k','wi','dr',) , $atts ) );
    
    	if ($atts = b) {
    		return '<img class="alignnone" src="/Bamboe%20' . ($b) . '.png" alt="" width="35" height="41" />';
    	}
    	if ($atts = t) {
    		return '<img class="alignnone" src="/Tekens%20' . ($t) . '.png" alt="" width="35" height="41" />';
    	}
    	if ($atts = k) {
    		return '<img class="alignnone" src="/Kringen%20' . ($k) . '.png" alt="" width="35" height="41" />';
    	}
    
    }
    add_shortcode( 'm', 'm_func' );

    But this does not work, can anyone help me on this subject?

    P.S.
    I read that attribute parameters require a default… but in my case there are no defaults… the attribute should only be used when it is entered…

Viewing 4 replies - 1 through 4 (of 4 total)
  • Thread Starter Perphide

    (@perphide)

    It seemed this option above eventually worked, but it could not handle multiple uses of the same type (b/t/k). I now have a temporary fix to shorten my previous 204 lines of almost identical code:

    function tiles_func( $attr = null, $content = null, $shortcode = null ){
    	if (!is_null( $content )) {
    	return '<img class="alignnone" src="'. get_bloginfo('template_directory') .'/images/mahjong%20tiles/' . $shortcode . '.png" alt="" width="35" height="41" />';
    	}
    }

    And for the shortcuts this did the trick to reduce the massive amount of code:

    for( $i = 1; $i <= 9; $i++ ) {
    	add_shortcode( 'b'. $i .'', 'tiles_func' );
    	add_shortcode( 'k'. $i .'', 'tiles_func' );
    	add_shortcode( 't'. $i .'', 'tiles_func' );
    };

    I hope someone else finds this useful, since I spent about 14 hours on this subject today and didn’t quite get the result I wanted.

    What isn’t the way you want it? What doesn’t work?

    Thread Starter Perphide

    (@perphide)

    It works this way…
    I am just wondering if this is the correct (and only) way…

    That is because I think it is weird to check for $content and then use $shortcode.

    Thread Starter Perphide

    (@perphide)

    Also there are two thing I currently can’t get working:
    1. Use an attribute called r and use it only when it is filled in by the user (I did find I can set the default value to null so it doesn’t do anything, but I did not find a way to check if the user did use the r attribute.

    2. This code makes shortcode like [b1] [b2] while at first I wanted this to be possible (especially the multiple uses of the same atribute:
    [m b=123 b=456 b=789]

    Side note: In the code above I have used ATTR instead of ATTS, maybe that is way a lot of testing late at night failed… however this is now fixed and $atts[0] still doesn’t work… so code above still my current actual code with the misspelling corrected.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Custom shortcode's’ is closed to new replies.