Notifications
Clear all

Limited Support

Our team is currently on holiday, so support will be limited during this period. Response times may be slower than usual, and some inquiries may be delayed.
We appreciate your patience and understanding, and we’ll resume our usual support by the end of August.

 

[Solved] Check for is_first_post when triggering custom shortcode from within post

9 Posts
2 Users
1 Reactions
554 Views
Posts: 47
Topic starter
(@golabs)
Trusted Member
Joined: 4 years ago

I created a shortcode that must only be usable from within the first post (is_first_post = 1).

The plugin is already fully working, I just need to add the check if it's being called from within the first post.

How can I do that?

I already had a look at:

$WPF     = WPF();
$WPF_posts = $WPF->current_object['posts'];

But that lists ALL the posts (so including replies) on the page/of the topic... but it doesn't tell me from within which post I called the information.

Anyone an idea how to get hold of just the details of the post from which the shortcode is triggered?

8 Replies
Sofy
Posts: 5483
 Sofy
Admin
(@sofy)
Support Team
Joined: 8 years ago

Hi,

Please try to clearly explain what exactly you're trying to achieve. If there's a simple solution, we'll share it here. 

Reply
4 Replies
(@golabs)
Joined: 4 years ago

Trusted Member
Posts: 47

@sofy I want the shortcode to only work from within the first post of the topic.

When I can obtain the metadata for the post the shortcode is located in / triggered from, then I can check for is_first_post = 1.

Reply
(@golabs)
Joined: 4 years ago

Trusted Member
Posts: 47

@sofy Does the additional info I provide help you to understand my situation? If not, please let me know. Unfortunately - I haven't been able to find a solution myself so far.

Reply
Sofy
 Sofy
Admin
(@sofy)
Joined: 8 years ago

Support Team
Posts: 5483

@golabs 

It'd be better if you could provide us more details. We're sorry, but we couldn't fully understand your request.

Reply
(@golabs)
Joined: 4 years ago

Trusted Member
Posts: 47

@sofy let me try again:

  • I have created a topic.
  • In the first post in the topic I want to be able to use a custom shortcode (e.g.
    [csv2table]
    demo,text,here
    123,456,789
    [/csv2table]

    ).

  • When rendering the csv2table shortcode I want to confirm that the shortcode is positioned in / triggered from the first post of the topic.

  • I know that the first post state is stored in the database in the is_first_post column (if first post than value is 1) in the wp_wpforo_posts table.

  • My question is: how can I obtain that is_first_post value in my csv2table shortcode so it will -only- transform the csv data (the shortcode content) into a table when it's being positioned in / triggered from the first post of the topic?
Reply
Posts: 47
Topic starter
(@golabs)
Trusted Member
Joined: 4 years ago

I've finally managed to get to a working solution (workaround?) but @sofy if you or another admin know of a better / more direct method then by all means share that solution with me/us.

 

First I capture the post array every time wpForo renders a post and make it available for shortcode to pick up later on:

// NOTE: the wpforo_content filter only passes two parameters: the rendered $content and the $forum array. There are no such parameters as $topic or $post.

add_filter( 'wpforo_content', 'scc_wpforo_forum_postmeta', 11, 2 );

function scc_wpforo_forum_postmeta( $content, $forum = [] ) {
    // store the raw post array to read later in the custom shortcode
    $GLOBALS['scc_wpforo_forum_postmeta'] = $forum;

    // now process ALL shortcodes in this content
    return do_shortcode( $content );
}
 
 
After that I have just my custom shortcode. In this example I've named it `csv2table` which turns csv data in the shortcode content into a table.
add_shortcode( 'csv2table', 'scc_csv2table_shortcode_handler' );

function scc_csv2table_shortcode_handler( $atts, $content = null ) {
    $atts = shortcode_atts(
        [
            'show_header' => true,
            'zebra_stripping' => true,
        ], 
        $atts,
        'csv2table'
    );

    // let's get the info from the stored $forum into $postmeta
    $postmeta = $GLOBALS['scc_wpforo_forum_postmeta'] ?? [];

    // when not the first post then return alternative output eg. a notification
    if ( empty( $postmeta['is_first_post'] ) ) {
        return '<div class="alert-information">Shortcode CSV2TABLE does not render in replies.</div>';
    }

    // otherwise render script to generate table from csv data provided in shortcode content

    // ... script to read the csv and turn it into a table, for the demo I replaced it with a simple text
    $table = "CSV2TABLE";

    return $table;
}

 

In the $forum array the following values are available:

[postid] => int  
[parentid] => int  
[forumid] => int  
[topicid] => int 
[userid] => int 
[title] => string
[created] => datetime 
[modified] => datetime
[likes] => int 
[votes] => int 
[is_answer] => int 
[is_first_post] => int 
[status] => int 
[name] => empty (/ string)
[email] => empty (/ string)
[private] => int 
[root] => int (can be negative)
[url] => string
[full_url] => string
[short_url] => string
[is_answered] => int
[likes_count] => int
[likers_usernames] => array

 

So that is almost all columns from the wp_wpforo_posts table and some from other tables.

Via the check on is_first_post I can determine whether or not the shortcode is positioned in / triggered from the very first post in the topic.

I could also use this to allow only certain userid's to use certain shortcodes.

What I would love to see possible as well in the future is:

  • if the post / reply is from the topic starter,
  • and the role of the user who posted the post / reply.
Reply
2 Replies
Sofy
 Sofy
Admin
(@sofy)
Joined: 8 years ago

Support Team
Posts: 5483

@golabs

I've contacted the developers regarding this code. They've reviewed your solution.
Here is their response:

“The solution is a good one. It is the best and most optimal approach for this case. No further improvements are required.”

Reply
(@golabs)
Joined: 4 years ago

Trusted Member
Posts: 47

@sofy thanks!

Reply