Author Archives: admin

Oddness in The WordPress Posts Loop and WP Query

While working on the new version of Shortcode Revolution we faced a couple of confusing behaviors of WordPress functions. I’ll document them here and will keep adding such information. Hopefully it can help other developers and save them a lot of time and frustration.

Excluding posts from WP Query (using post__not_in)

There is a good comment here explaining that post__not_in wouldn’t work when used with post__in in the same query. This is a good note but our query did not include post__in and post__not_in was still simply ignored. I did not dig deep into the code to see why this happens – the conclusion was simply not to use it.

Here is an example how we get 3 random related posts in Shortcode Revolution:

$query = [];
// we add 1 to the default number of posts which is 3 or to the user-passed number or posts
$query['posts_per_page'] = (empty($atts['num']) or !is_numeric($atts['num'])) ? 4 : intval($atts['num']) + 1;       
$query['orderby'] = 'rand';

// getting the posts here
$wp_query = new WP_Query($query);
$posts = $wp_query->posts;

// now because this is "related posts" query, we don't want the current post to be repeated below it. So let's filter it out
// $post_id is a variable we have defined earlier in the shortcode and it contains the current post ID
// we are using array_filter with a closure here. You can do it inside a loop with a counter but this solution is more elegant
$posts = array_filter($posts, function($p) use($post_id) {				
  return ($p->ID != $post_id);					
});

// if the current post was not there we have one more than needed
if($query['posts_per_page'] < count($posts)) array_pop($posts);

 

So that’s it – you just get more posts than you need and then remove the unwanted ones in PHP.

Showing the excerpt outside of the post loop – get_the_excerpt

If you look at the documentation it sounds like you can call get_the_excerpt with any post ID and receive the excerpt for that post id. Good luck! You may get the proper excerpt (sometimes!) but won’t get the automatically generated “read more” link properly. The first version of our code was this and it did not work:

foreach($posts as $p):
   $background_image =  has_post_thumbnail( $p->ID )  ?  get_the_post_thumbnail_url($p->ID) : ''; 
   $excerpt = get_the_excerpt($p->ID);
.....

I intentionally did not use $posts as $post to avoid overriding the global $post variable but apparently this was wrong. Fortunately this comment gave me the hint how to fix it:

foreach($posts as $p):
   // yes, do override the global!
   $post = $p;
   // and you need to setup_postdata($post)
   setup_postdata($post);
   $background_image =  has_post_thumbnail( $p->ID )  ?  get_the_post_thumbnail_url($p->ID) : ''; 
   $excerpt = get_the_excerpt($p->ID);
......
endforeach;

And after the end of the loop don’t forget to reset the post data:

wp_reset_postdata();

 

Zapier Webhooks in Namaste! LMS

From version 2.5.6 the WordPress learning management sytsem Namaste! LMS supports webhooks & Zapier integration.

You can learn more about Zapier’s implementation of webhooks here.

Currently Supported Actions

At the moment Namaste! LMS allows you to attach hooks to the following actions:

  • Student enrolls in a course. This happens for auto-approved enrollments or enrollments made by admin. It will also be fired when a pending enrollment is approved.
  • Student completes a course.

Webhooks can be used to connect your LMS to mailing list services, CRM, or any other web based services that support webhooks directly or through Zapier (or similar products).

To get to the webhooks interface click on the Webhooks / Zapier link in your Namaste! LMS menu. Here is the interface that you’ll see when adding a webhook:

A webhook form for Namaste LMS

With the above example you will have your webhook notified when someone successfully enrolls in the Free course. The hook will send username, email, and 3 fixed custom parameters. The data that will be sent to Pipedream (you can use it for testing and more) is this:

data sent to Pipedream

 

Pipedream lets you test your hooks and see the JSON message that you have sent to them. In real usage this data will be used by other apps to do all kind of things with it.

Stay tuned for more webhook events powered by the Namaste! Connect module soon.

Using the Data Shortcodes in Shortcode Revolution

The Data Shortcodes tab in the Shortcode Revolution plugin is simple but powerful way to build profile pages, greetings, widgets with user stats, and so on.  It has 3 ways to specify the user whose data we’ll be showing:

Currently Logged User

Probably the simplest and most frequent use will be to get data for the currently logged in user and do something with it. A simple example is just a greeting:

Getting a shortcode for first name

You can get this shortcode and place it in a widget like this for example:

Hello, [srevo-profile user_id=”logged_in” field=”first_name”]!

Which will produce Hello, John! or whatever the user name is. So far so good.

If you want to use it inside your theme instead of a widget you need to use do_shortcode. Like this:

<?php echo do_shortcode(‘Hello, [srevo-profile user_id=”logged_in” field=”first_name”]!’);?>

This is simple and easy.

Fixed User IDUsing the shortcode for a fixed user ID

This usage is less common inside a post or widget because you will rarely have such fixed content for each of your users in the system. It is however very useful if you are doing something with programming in your theme. In this case you can dynamically pass the user ID. Something like this:

<?php echo do_shortcode(‘[srevo-profile user_id=”specific” user_id=”‘.$current_user_id.'” field=”meta_account_status”]’);?>

* Please note that the above example assumes you have created the variable $current_user_id. If not, you can use the built-in WP function get_current_user_id().

User ID Passed by URL Parameter

This is the most powerful way to use the data shortcodes and it works great when you are creating dynamic profile pages or other pages which display data for all users.

Using the shortcode with a GET parameter

In this example we are building a profile page (obviously we have not formatted anything in the enclosed content in the shortcode. But you can nicely format a table or other user interface with colors, font formatting, etc.

The important thing here is the variable name field. You can use any variable – in our example it is called “uid”, but you can use “user_id”, “member_id” or whatever you like. You can then load the profile data by passing this variable in the URL of the page where you have published the shortcode. For example let’s assume you have published the shortcode in this URL:

https://blog.calendarscripts.info/user-profile/

Then going to https://blog.calendarscripts.info/user-profile/?uid=25 (if you have used the variable name “uid”) will show the data for user with ID 25.

Let’s create a very simple example of how you can use this in a page that lists all users in your system. The page could be created with a custom shortcode or a theme function:

<?php
// let's assume you have existing array of members in $members
foreach($members as $member) {
   echo '<p><a href="'.add_query_arg(['uid' => $member->ID], 'https://blog.calendarscripts.info/user-profile/').'">'.$member->display_name.'</a></p>';
}

That’s it. Simple and powerful.