0

I am wondering if there is a way to take content between shortcodes and input it into a string, for example, I want to be able to have [shortcode]Hello World![/shortcode] input to a string called $ShortCodeText, how do I do this?

  • Welcome to WPSE. It looks like your code got some syntax errors. Can you elaborate what you mean by "content". I wonder if you mean: [shortcode1]...[/shortcode1] ...GET THIS CONTENT...[shortcode2]...[/shortcode2] or do you mean something else? – birgire Jun 18 '14 at 19:02
  • I edited his post so that the syntax is corrected, just waiting on the edit queue... He May also mean [shortcode1][shortcode2]Hello Word!![/shortcode2][/shortcode1], now that I see your comment... hmmm – eyoung100 Jun 18 '14 at 19:08
  • That isnt what a shortcode is for you'll need some custom query's with a date stamp, unless your going to manually input ads with a short code. – eyoung100 Jun 18 '14 at 19:14
1

An Example Shortcode:(Taken from: GenerateWP: Shortcodes Generator

// Add Shortcode
function img_shortcode( $atts , $content = null ) {

    // Attributes
    extract( shortcode_atts(
        array(
            'width' => '',
            'height' => '',
        ), $atts )
    );
    // Code
// This is the line you need to study:
return '<img src="' . $content . '" width="' . $width . '" height="' . $height . '">';

}
add_shortcode( 'img', 'img_shortcode' );

This is the default [img] tag used by phpBB and others as a "shortcut" to posting an image. Notice that to return HTML you must do it in raw format. You cannot stack the output across multiple functions, i.e. Pass function 1 to function 2, and expect function 2 to be overloaded with function 1's content.

UPDATE

Using the example I gave you earlier, I scribbled this together, I can't promise it will behave the way I expect, nor is there a way to add images except manually:

// Add Shortcode
function automatic_ads_shortcode( $atts , $content = null ) {

    // Attributes
    extract( shortcode_atts(
        array(
            'width' => '',
            'height' => '',
        ), $atts )
    );
    // Code
    // Set the $image_array base_dir to the Media Library Path
    define('IMAGES_PATH', dirname(realpath('/wp-content/uploads/')));

    //Ad Rotator 
    //Array of Ads
    //Manually add your ad images to this array. Due
    //to the functionality of a Shortcode, there is no
    //way to accept user input.  You'd need a plugin
    //for that. 
    $image_array = array(IMAGES_PATH . 'img1.jpg',
                         IMAGES_PATH .'img2.jpg',
                         IMAGES_PATH .'img_x.jpg');             
    $current_week = date(W);

    // Shuffling + Random ensures we don't
    // get the same image twice in a row.
    // Every 8 Weeks = 2 months
    if ($current_week % 8 == 0)
    {
        $shuffled_array = shuffle($image_array);
        $chosen_image = array_rand($shufled_array,1);
    }
    //Every 4 Weeks = 1 month
    elseif ($current_week % 4 == 0)
    {
        $shuffled_array = shuffle($image_array);
        $chosen_image = array_rand($shufled_array,1);

    }
    // All other Cases
    else {
        $chosen_image = array_rand($image_array,1); 
    }
//Output Line
return '<img src="' . $chosen_image . '" width="' . $width . '" height="' . $height . '">';

}
add_shortcode( 'auto_ads', 'automatic_ads_shortcode' );

After Testing this, use the shortcode [auto_ads] and add said shortcode in the section where the ads normally appear. As you can see, I stick by the idea that you cannot stack output.

  • Why Not use an Ad Plugin? – eyoung100 Jun 18 '14 at 19:22
  • I'll help you, if you rephrase your question above and we can do this with a shortcode – eyoung100 Jun 18 '14 at 19:38
  • The question has been rephrased – Tom Jun 18 '14 at 19:45
  • @User I think this will do the trick – eyoung100 Jun 18 '14 at 20:48
  • Thanks for the +1, but please test it 1st... if it's broken, get in touch here, and we'll fix it – eyoung100 Jun 19 '14 at 2:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.