0

A beginner developer here, working on my first project. I've been trying to find some guidance on how to create a plugin or function to create a specific shortcode.

Let's say I have two default texts prepared; in them, one %word% repeats throughout the text. When inserting the shortcode, I'd like to specify what word should replace that %word% in my default texts.

So let's say I want to insert Text No#1 at the end of my blog post. I imagine the shortcode should look something like this: [Text1 word="Name";] and this would insert the prepared Text with %word% replaced with Name throughout the text.

I hope you'll understand what I have in mind. I don't ask for code - I'd really appreciate if you'd help me to get on the right road, explanation of steps in order would be highly, highly appreciated. I have millions of questions - in what form should I save those texts? How do I start creating the custom shortcode? I plan testing on localhost environment (which I have set up), if that's okay. Thank you!

  • a shortcode cannot be used to modify text throughout an article. It can only really be used to output content specifically in the area it's inserted. It wont allow you to change the words on the article that are not directly related to the shortcode. Are you planning to wrap the entire article in a shortcode? – Paul G. Jun 15 at 21:54
  • I do not want to modify text in article; I want to modify the text template whenever I insert it using a shortcode. Slightly alter the template for each insertion. Lets say my template text looks like this: #name# does something; then #name# does something and somethinh etc. So when I insert a shortcode, I want to replace #name# with some specific name :) – Newbie1 Jun 16 at 22:06
0

You can use somthing like this:

function my_custom_shortcode( $atts ) {

    // Attributes
    $atts = shortcode_atts(
        array(
            'word' => 'example', // the word which will be replaced
            'part' => '1' // for identifing the different texts
        ),
        $atts,
        'textblock'
    );

    // only if a word exists
    if ( isset( $atts['word'] ) ) {

        // see if part="1" was inserted in the shortcode...
        if ($atts['part'] === '1') {

            $text = 'Lorem ipsum dolor sit amet, '.$atts['word'].' sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna '.$atts['word'].' erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est '.$atts['word'].' sit amet.';

        } else { //... if not use this text

            $text = 'At vero eos et accusam et justo duo dolores et, '.$atts['word'].'.';

        }

        return $text; // return the text
    }


}
add_shortcode( 'textblock', 'my_custom_shortcode' );

We use a shortcode with 2 attributes. One attribute is the word which you want to replace. The second attribute (part) is for identifing which static text you want to show.
As you see this will only work with static text which you entered in the shortcode before.

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.