Do I need to escape number in this shortcode function?

Yes, never trust user’s input. Just because you told people to provide a valid number for a specific shortcode parameter, it doesn’t guarantee that the input will always be a valid number, so always secure user’s input — and output. You should also, if you haven’t already done so, read these articles: Data Validation Securing … Read more

Implementing a URL Shortener

Step 1: Create a new template file in your themes folder named: shorturl.php <?php /* Template Name: Short URLS Template Post Type: post, page */ while ( have_posts() ) : the_post(); $content=get_the_content(); header(‘Location: ‘.str_replace(“&amp;”,”&”,$content)); exit(); endwhile; Step 2: Create a new php file in your wp-content/plugins/ folder and name it: public-url-shortener.php <?php /* Plugin Name: … Read more

enqueue script with get_shortcode_regex

There’s already a function for checking if a post has a shortcode: has_shortcode(): function enqueue_script_if_shortcode_is_detected() { global $post; wp_register_script( ‘leweb-widgets’, get_stylesheet_directory_uri() . ‘/includes/leweb-widgets.php’, ‘1.0’, true ); if ( has_shortcode( $post->post_content, ‘leweb_suggested_events’ ) ) { wp_enqueue_script(‘leweb-widgets’); } } add_action( ‘wp_enqueue_scripts’, ‘enqueue_script_if_shortcode_is_detected’ ); Just keep in mind that checking the post this way: Will not know if … Read more

How to add shortcode to html widget

First thing you need to do is drag & drop a Text widget to your WordPress sidebar on the Appearance » Widgets screen in your dashboard. After adding the widget, you can simply add your shortcode inside the text edit area of the widget.

Shortcode Always Displays Default Attribute Value

The quotes in the shortcode call were not standard (despite them looking so). Replacing the old double quote characters with typed-into-notepad, copied, and pasted new quotes in the theme editor resolved the problems. Thanks to @WebElaine and @vancoder for the help!

How to transform WordPress user role code to WP shortcode?

You can add a parameter to your shortcode function and use this value to check if the user is allowed to view the content: function func_check_user_role( $atts, $content = null ) { $user = wp_get_current_user(); $user_role = $atts[‘role’]; $allowed_roles = []; array_push($allowed_roles , $user_role); if ( is_user_logged_in() && array_intersect($allowed_roles, $user->roles ) ) { return $content; … Read more

Hijack WordPress Shortcode attribute

You have several options, but I would advise against what you’re doing, what you really need is to make a new shortcode that calls the plugins functions to achieve what you want ( or to use a plugin that provides a filter ): 1. Search Replace on the Post Content Hook into the_content filter, and … Read more

How to check if a post has any one of many shortcodes?

So yes, as you say you can’t pass an array to has_shortcode, which is confirmed in the docs, so you need to do the ‘OR’ operation manually, which would look something like this: $shortCodesToTest = [ ‘wpdocs-shortcode-1’, ‘wpdocs-shortcode-2’, ‘wpdocs-shortcode-3’]; $anyShortCode = false; foreach ($shortCodesToTest as $sc) { if (has_shortcode( $post->post_content, $sc) ) { $anyShortCode = … Read more

A shortcode nested inside a shortcode

This should work: function cpa_haven_banner() { $shortcode = do_shortcode(“[get_sheet_value location=’LOAN!B7′]”); return ‘ <ul class=”list-group list-group-horizontal entry-meta”> <li class=”list-group-item list-group-action-item”> <strong>Female</strong> ‘ . $shortcode . ‘</li> </ul> ‘; } add_shortcode(‘cpa-haven’, ‘cpa_haven_banner’);