functions - Shortcode to insert default text and change one word throughout it?

A beginner developer here, working on my first project. I've been trying to find some guidance on how to create a p

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 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!

Share Improve this question asked Jun 15, 2019 at 13:29 Newbie1Newbie1 132 bronze badges 2
  • 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. Commented Jun 15, 2019 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 Commented Jun 16, 2019 at 22:06
Add a comment  | 

1 Answer 1

Reset to default 1

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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745393436a4625769.html

相关推荐

  • functions - Shortcode to insert default text and change one word throughout it?

    A beginner developer here, working on my first project. I've been trying to find some guidance on how to create a p

    17小时前
    30

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信