• Resolved mirrorside

    (@mirrorside)


    Hello.
    I want to have a shortcode, where after every 20th word there should be inserted a string: “This post is from Moderator XXX”.

    [moderatortext] word1-19 “This post is from Moderator XXX” word1-19 ….[/moderatortext]

    I want this Shortcode exclusively used by my Moderators. This I know how I can do/limit.
    But I am clueless about the Shortcode implementation itself.

    Some simple Shortcodes I’ve already created, so I know the API.
    But I am not a PHP expert, and with this special Shortcode I am stuck.

Viewing 2 replies - 1 through 2 (of 2 total)
  • I do not think this is easy, and maybe not even possible, because of embedded punctuation and possible html. However, the code below takes a simplistic approach and splits the content on the spaces class:

    function mam_moderated_func ($atts,$content=null) {
    	extract(shortcode_atts(array(
             'noatts' => 'Unknown',
                                    ), $atts));
    	$output = '';
       if (!is_null($content)) {
          preg_match_all('/((\s+)?(\S+))/',$content,$matches);
          foreach ($matches[0] as $match) {
             $output .= $match;
             if (++$count == 20) {
                $output .= ' MODERATED ';
                $count = 0;
             }
          }
       }
       return $output;
    }
    add_shortcode('mam_moderated','mam_moderated_func');
    Thread Starter mirrorside

    (@mirrorside)

    It has been late, but thank you very much.
    It is exactly what I intended and it works fine.

    Thank you again for your help.

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘Shortcode help’ is closed to new replies.