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?
Hi,
Please try to clearly explain what exactly you're trying to achieve. If there's a simple solution, we'll share it here.
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 ); }
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.