Category Archives: CalendarScripts

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();

 

Shortcode Revolution for WordPress

During our work on various WordPress installations I have figured out that we repeatedly create (oh well, kind of copy/paste) layout elements, dynamic stuff like lightboxes and sliders, pieces of code in custom plugins, and so on.

Some of these codes slowly turned into a library so we decided to go ahead and package them into a new free plugin called Shortcode Revolution. (You can check it at our site or directly at the WordPress repo)

This is just a very first version so there is hopefully a lot more to come. But let’s have a brief look at what’s available:

Posts, Related Posts, and Comments

UI for posts / comments shortcode

This allows you to create widgets that contain posts related to a topic, searched by ID or tag, related to the currently shown post, and so on.

There are 3 handy layout modes: simple list, default (with a thumbnail and excerpt), and a carousel / slider.

And above this paragraph you see a slider / carousel of 3 posts related to this one.

Popup / Lightbox

modal box UI

This is such a frequent task, and this plugin really makes it easy. Just enter your contents, some clickable text and probably some custom CSS classes. Voila, the popup is created.

Click me!

Columns and Grids

Columns / Grids UI

I mean, it’s typical that the content on the web normally just stays in one column. But who said it should always be that?

Do you remember newsletters? I hope our readers are not all that young. Newsletters (the paper ones!) typically have content flowing in multuple columns. That’s not typical for the web not because it’s bad but because it was hard thing to do in the past.

Now with the new CSS techniques it’s not that hard. But you don’t always want to write CSS especially when you are writing a blog post, do you? This is where the columns shortcode comes handy!

The shortcode also allows you to create a grid. In the grid the content does not flow in the columns. Instead, each piece stays in its own grid cell. Like this:

Hey, I’m the first item

And I am number two

They can all be different

And can contain images (look right)

Chart from a personality quiz

This at left is from our quiz plugin WatuPRO

Third line, item 1

And number two

The ninth item goes here

Tabs

Tabs UI

Tabs allow you to place content within, well, tabs. What does it mean? Look at this:

As I told you, you can  arrange content in tabs 🙂 Just click on one of these tabs and you’ll see the content related to each one.
just a chartLet’s just have a chart here to look smart.

Buttons

Create buttons UI

There isn’t so much to explain here. You get an easy to use UI to create all kind of buttons.

Tables

create tables from CSV

This one is super simple – it creates table from an uploaded CSV file. Technically you can use a remove or a dynamic CSV as well – just replace the uploaded file URL with the remote one.

Tables aren’t that much fascinating – I am sure you have seem some! So no demo for this shortcode.

Flashcards / Flipcards

Create flashcard shortcodes

There cards have a front and a back side and reveal the back when the user clicks on them. Simple:

Here’s a Math task:

2 + 2*3?

Of course it’s 8 because multiplication has a higher priority than addition 🙂

Who is she?

Adriana Lima

You can even combine shortcodes together like I did above. I placed two flashcard shortcodes in a grid so they can be side by side.

Data Shortcodes

creating a data shortcode

They are excellent for creating profile pages in communities, author pages, widgets containing some user data, and so on.

Custom Shortcodes

Just any content that you may want in many places on your site. Custom HTML, shortcodes, media, formulas, whatever. Easily repeatable and editable in one place.

Enjoy the plugin – it’s free and can save you many hours of work. Let us know your feedback, feature requests, and bug reports (yes, even bug reports!).

Arigato PRO 3.2

The new version of Arigato PRO is here with a lot of improvements and fixes:

  • Added “% unsubscribed” users stats for every mailing list.
  • Added option to mass-pause email automation messages.
  • Formidable Forms integration available. More info: https://blog.calendarscripts.info/formidable-forms-integration-in-arigato-pro/
  • You can now send Webhooks to Zapier or other services when someone subscribes to or unsubscribes from a mailing list.
  • Added a configurable option to store and show subscribers IP address. Do this at your own responsibility depending on your local privacy law.
  • “Send after X o’clock” is now available also for newsletter emails.
  • Added option to automatically log in the user to the site when auto-subscribing them as WP user.
  • Added option to mass activate / deactivate mailing list subscribers.
  • The unsubscribe reason from the unsubscribe poll (if any) will be included in the optional unsubscribe notification email message.
  • The Settings page has been reorganized in logical tabs.
  • Added a shortcode to display a public newsletter archive. You will find the shortcode on Your Newsletters page.
  • Optional tags added to subscribers data for searching in the administration. The tags can also be used in segmentation if you have the Intelligence module installed.
  • Implemented Google reCaptcha v3.
  • Values can be passed as URL parameters to prepopulate a subscribe form. Learn how: https://blog.calendarscripts.info/passing-values-through-url-parameters-in-arigato-pro/
  • Improved handling of emojis in email messages.
  • Custom presets can now be stored in wp-content/uploads/arigatopro-presets folder so you don’t lose them on plugin updates.
  • The raw email log now can show the emails for a start / end date interval.
  • A hook added when an email is sent.
  • Added validation for file type when uploading subscribers.
  • Improvements to handling daily mail limits.
  • Added personal data eraser for GDPR compatibility.
  • Removed session usage for displaying on-screen messages.
  • Displayed field name for a second method of integrating custom fields in Contact form7. This way you can make the fields validate as required and also have these fields sent in the email message that CF 7 sends to you. It will also make the field get saved in Data Tensai.
  • Dynamically get the DB collation when the plugin is activated.
  • Added optional last name to the Contact form 7 integration.
  • [Gozaimasu module] Added A/B tests on message subjects. Learn more at https://blog.calendarscripts.info/a-b-testing-of-email-message-subjects-in-arigato-gozaimasu/
  • [Gozaimasu module] The “sticky bar / ribbon” design now also have option for showing only after the visitor has spent some time on the site.
  • [Fixed bug] After reordering the Settings page changes made in it were not being saved properly.
  • [Fixed bug] Name field was not showing an asterix when it was a required field.
  • [Fixed bug] Checking only some checkboxes on Manage Subscribers page did not reveal the mass action buttons below the table.