Shortcodes: Pros and Cons

An alternative you have not mentioned is to customize the shortcode experience, since shortcodes are a UX failure for the end user. Create a specific shortcode button on the editor so the user can select text and click an actual button. You can go further and attach some code to error check what the user … Read more

Hide Post comments when displayed via WP_Query

The problem You must remember to call the core function wp_reset_postdata(), after your while loop, to restore the global $post object. The comment form is relying on that object, that you override with your $queryPosts->the_post() call. Note that the extract() isn’t recommended, check this answer by @toscho, for example. Removing comments To remove the comment … Read more

Adding PHP/HTML code inside page from custom template

This is how to code the shortcode function foobar_func( $atts ){ return “foo and bar”; } add_shortcode( ‘foobar’, ‘foobar_func’ ); And to execute a function using output buffering add_shortcode( ‘shortcode_tag’, ‘function_name’ ); function function_name($atts) { ob_start(); // Add your code $var = ob_get_contents(); ob_end_clean(); return $var; }

Only execute jQuery function(on document ready) on the page has shortcode from plugin [duplicate]

You can wp_enqueue_script in your shourtcode. Here are the code I fixed. Register your JS file function wh_enqueue() { wp_register_script( ‘wh-ajax-app’, WH_PATH . ‘/js/write-here-ajax.js’, array(‘jquery’), ‘1.0.0’, true ); } add_action( ‘wp_enqueue_scripts’, ‘wh_enqueue’ ); and enqueue in your shortcode function function ajax_write_here(){ wp_enqueue_script( ‘wh-ajax-app’ ); // Enqueue your script here $output=”<div id=”write_here_ajax_wrap”></div>”; return $output; } add_shortcode(‘write-here-ajax’, … Read more

Wrap each shortcode in array to div

You need to recurse into the shortcode content ($m[5] as returned by get_shortcode_regex()), the standard way being to use preg_replace_callback(): public function wrapShortcode( $content ) { $content = preg_replace_callback( “https://wordpress.stackexchange.com/” . get_shortcode_regex() . ‘/s’, array( $this, ‘wrapShortcodeCallback’ ), $content ); remove_filter( ‘the_content’, array( $this, ‘wrapShortcode’ ), -9999999 ); // Stray spaces can create lost paragraph … Read more

Remove and restore one shortcode

Another old unanswered question. Hopefully useful to someone in the future. WordPress stores shortcode tags and callbacks in the $shortcode_tags. This is how I’d do it. function my_the_content( $content ) { global $shortcode_tags; $tag= ‘some_shortcode’; //* Make sure it’s actually a valid shortcode if( ! isset( $shortcode_tags[ $tag ] ) ) { return $content; } … Read more

Editing built in Gallery shortcode to filter by categories

There are various ways to run another WP_Query with a tax query and collect the ID’s into the include attribute of the gallery shortcode. Let’s try another approach, without running a secondary WP_Query. Native gallery: Support taxonomy and term attributes We introduce the following custom gallery shortcode attributes: Here’s a demo plugin that supports that … Read more

How to exclude shortcode from specific page IDs if it’s set to global

Please try this and let me know the result function my_shortcode_to_a_post( $content ) { global $post; $disabled_post_ids = array(’20’, ’31’, ’35’); $disabled_tag_ids = array(‘5′, ’19’, ’25’); $current_post_id = get_the_ID(); // try setting it to $post->ID; if it doesn’t work for some reason if(in_array($current_post_id, $disabled_post_ids)) { return $content; } $current_post_tags = get_the_tags($current_post_id); if($current_post_tags){ $tags_arr = array(); … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)