Count the number of times the WordPress shortcode is used in a post

I have the following WordPress shortcode function:

function wp_shortcode() {
 static $i=1;
 $return = '['.$i.']';
 $i++;
 return $return;
}
add_shortcode('shortcode', 'wp_shortcode');

      

This works great. The variable $i

is incremented every time the function is called in the article. So for

[shortcode][shortcode][shortcode][shortcode]

      

the output will be as expected

[1][2][3][4]

      

but

if the article contains more than one page than the counter is reset on the next page of the article. So for:

[shortcode][shortcode][shortcode][shortcode]
<!--nextpage-->
[shortcode][shortcode][shortcode][shortcode]

      

output

[1][2][3][4]
<!--nextpage-->
[1][2][3][4]

      

instead of the desired output:

[1][2][3][4]
<!--nextpage-->
[5][6][7][8]

      

Is there anyway to change this?

+3


source to share


4 answers


So, after exploring a slightly more complex idea with preprocessing the_content and adding $ atts to shorcodes before showing the shortcodes, I came up with the following:



function my_shortcode_content( $content ) {
 //check to see if the post has your shortcode, skip do nothing if not found
 if( has_shortcode( $content, 'shortcode' ) ):

  //replace any shortcodes to the basic form ([shortcode 1] back to [shortcode])
  //usefull when the post is edited and more shortcodes are added
  $content = preg_replace('/shortcode .]/', 'shortcode]', $content);

  //loop the content and replace the basic shortcodes with the incremented value
  $content = preg_replace_callback('/shortocode]/', 'rep_count', $content);

 endif;

return $content;
}
add_filter( 'content_save_pre' , 'my_shortcode_content' , 10, 1);

function rep_count($matches) {
 static $i = 1;
return 'shortcode ' . $i++ .']';
}

      

+2


source


Good question. A possible solution is below, however not entirely elegant, because it requires you to have a fixed number of shortcodes per page, for example. 4:



add_shortcode( 'shortcode', 'wp_shortcode' );
function wp_shortcode() {
    global $page;
    $spp = 4;
    static $i = 1;
    $ii = $i + ( ( $page - 1 ) * $spp );
    $return = '<a href="#f'.$ii.'" name="r'.$ii.'">['.$ii.']</a>';
    $i++;
    return $return;
}

      

+1


source


This is because on every page load, you redefined your function, so it static $i=1;

will always be executed.

You might want to use a variable $_SESSION

. But don't forget session_start()

somewhere in the callback init

.

function wp_shortcode() {
    if (empty($_SESSION["myCounter"])) {
        $_SESSION["myCounter"] = 1;
    }
    $return = '<a href="#f' . $_SESSION["myCounter"] . '" name="r' 
        . $_SESSION["myCounter"] . '">[' 
        . $_SESSION["myCounter"] . ']</a>';
    $_SESSION["myCounter"]++;
    return $return;
}
add_shortcode('shortcode', 'wp_shortcode');

      

When ending the session, place it in functions.php

add_action('init', 'my_init');
function my_init() {
    if (!session_id()) {
        session_start();
    }
    //Other init here
}

      

0


source


If you just need to count the common tags on a page / page you can use this:

function count_shortcodes( $content, $tag ) {
    if ( false === strpos( $content, '[' ) ) {
        return 0;
    }

    if ( shortcode_exists( $tag ) ) {
        preg_match_all( '/' . get_shortcode_regex() . '/', $content, $matches, PREG_SET_ORDER );
        if ( empty( $matches ) )
            return 0;

        $count = 0;
        foreach ( $matches as $shortcode ) {
            if ( $tag === $shortcode[2] ) {
                $count++;
            } elseif ( ! empty( $shortcode[5] ) && has_shortcode( $shortcode[5], $tag ) ) {
                $count++;
            }
        }

        return $count;
    }
    return 0;
}

      

0


source







All Articles