WordPress.org

Ready to get started?Download WordPress

Forums

Shortcodes Pro
[resolved] shortcode to php functions (and beyond) (2 posts)

  1. devo.uk
    Member
    Posted 1 year ago #

    Hi

    I been looking for a quick solutions to allow me to code a bit quicker with images within the page and post editor

    i have a theme and in which a image directory lies
    wp-content/theme/images

    i want to basically upload site images manual when i am doing my design then use shortcode pro to allow me to tag and insert the
    <?php bloginfo('template_directory');?> snippet in to the editor thus making a standard "site dynamic" shortcode, i can use for any theme over and over again

    stage 1 : just get the image in to the page
    thus in the editor

    [timg]logo.png[/timg]

    would produce output (user browser)

    <img src="http://demosite.co.uk/wp-content/theme/image/logo">
    in the browser

    the second stage to take this further is having parameters for alt="" title="" width="" height=""/> being allowed at the start of the shortcode

    finally the 3rd stage would be to allow the setting at the time of the shortcode for the parameters after, but if no value is entered then it basically removes them from the output

    the 4th stage if the paramaters settings can be set to optional tickbox at the time of designing the code for good enforcement

    ie all images should have an alt value, even if it is not entered at the time of entering the shortcode then it would still create

    <img src="http://demosite.co.uk/wp-content/theme/image/logo" alt="" />

    even if no value was specified through the parameters section it would render as above so at testing stage you would be reminded of all the places where the tag needed to be filled in, in this case to help with seo

    not asking much am i

    would love to hear your help and views including matts :)

    regards

    devo

    http://wordpress.org/extend/plugins/shortcodes-pro/

  2. devo.uk
    Member
    Posted 1 year ago #

    ok after 3 days of looking at this and trying to work out what i could and i cant do within the realms of the php

    I found that some functions of wordpress and php would cause the code to crash.
    the use of

    $templatedir = bloginfo('template_directory')
    result = shortcode pro to crash or just display the template directory at the top of the page

    but after reading for an alternative i found the

    $templatedir = get_bloginfo('template_directory')
    result = allowed result for string maipulation

    i presume how certain functions have to be parsed before the rendering of the page / post and some can be evoked as the page / post text is being built which would be after a header and above is processed

    i know the code is not the most tightest but thought that it will give people an insight

    The code fills in

    • relative root to image (no domain prefix)
      from attributes set up
    • adds the correct alt tag or a blank one if none is entered
    • the chance to add height or width or both on the image size
    • tidies up the output string so that there is no extra blank spaces between parameters

    custom code = php
    attributes alt / alt description / text
    attributes height / height size % or px / text
    attributes width / width size % or px / text

    if ($atts["alt"] =='alt=""') { $alt='';} else { $alt = 'alt="'.ltrim($atts["alt"]).'"'; }
    
    if ($atts["width"] ==' ') { $width='';} else { $width = 'width="'.ltrim($atts["width"]).'"'; }
    
    if ($atts["height"] ==' ') { $height='';} else { $height = 'height="'.ltrim($atts["height"]).'"'; }
    
    $params = $alt.' '.$width.' '.$height;
    
    $returnstring = get_bloginfo('template_directory'). '/images/' .$content.'" '.$params);
    
    return '<img src="'. str_replace('  ',' ',$$returnstring).' />';

    hope this helps someone and gives a guide of how to use the custom code and what pitfalls you may come up against.

    i think most people would not take this long to resolve an issue as it would be a fine line of the above and then manually hardcoding shortcode functions but i wanted to see it through

    Great work matt with the plugin :)

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic