Skip Navigation

[Resolved] add shortcode to all custom posts

This support ticket is created 5 years, 4 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 -
- 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 -

Supporter timezone: Europe/London (GMT+00:00)

This topic contains 10 replies, has 2 voices.

Last updated by davidm-13 5 years, 4 months ago.

Assisted by: Nigel.

Author
Posts
#1306833
chosen.JPG

Hi, I'm using Chosen plugin to create pretty select dropdown boxes on a custom post type.
To activate it I need [chosen] to appear on the page see attached screenshot.
How can I add [chosen] to all custom posts automatically

#1306911

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

You want this shortcode added wherever any post is displayed on the front end?

In which case add it to the templates you create to display these posts (whether that it Content Templates or Layout Templates).

#1307093
chosen1.JPG

Hi Nigel,
That doesn't work - [chosen] styling isn't applied.
Only works if I enter [chosen] into the content area of the post as shown in screenshot above.
Attached screenshot of how you suggested - is that what you meant?

Thanks
David

#1307577

Any ideas?

#1307631

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Yes, that's what I meant, and if you were to define a simple custom shortcode to return some text it would work equally whether you added the shortcode to the template for the posts or to the posts themselves.

So if your shortcode doesn't work when added to the template, well... I don't know what the shortcode does or how it is coded.

Can you share the PHP code used to define the shortcode?

#1307743

This is the github page: hidden link

One solution for me is to load it with the csv file, putting [chosen] in the csv_post_post field, but wanted to avoid reloading them. These are parent post, I've loaded the associations records, so if I was to reload them would they still connect to their child posts - assuming there's been no change to any of the titles?

Thanks
David

#1307867

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

OK, the handling of the shortcodes is atypical, I've modified how the shortcode is coded to enqueue the scripts and styles required so that it should also work when used in a template.

This is the main chosen.php plugin file, modified:

/*
Plugin Name: Chosen for WordPress
Plugin URI: http://wordpress.org/extend/plugins/chosen/
Description: Chosen makes long, unwieldy select boxes much more user-friendly. There are no settings. Chosen applies itself automatically.
Author: Brent Shepherd
Version: 0.3
Author URI: <em><u>hidden link</u></em>
License: GPLv2 or later
*/

WP_Chosen::init();

class WP_Chosen {

	/**
	 * URL to the directory housing Chosen Javascript files.
	 */
	public static $chosen_url;

	/**
	 * URL to the directory of this plugin
	 */
	public static $wp_chosen_url;


	/**
	 * Setup the class variables & hook functions.
	 */
	public static function init() {
		
		self::$wp_chosen_url = plugins_url( '', __FILE__ );
		self::$chosen_url    = plugins_url( 'chosen', __FILE__ );

		add_action( 'wp_print_scripts', __CLASS__ . '::maybe_enqueue_scripts' );

		add_shortcode( 'chosen', __CLASS__ . '::shortcode_handler' );
	}


	/**
	 * If the post/page contains a select element, enqueue the chosen & jquery scripts.
	 */
	public static function maybe_enqueue_scripts() {

		if ( self::contains_select() && ! is_admin() ) {
			wp_enqueue_style(  'chosen', self::$chosen_url . '/chosen.css' );
			wp_enqueue_script( 'chosen', self::$chosen_url . '/chosen.jquery.min.js', array( 'jquery' ), false, true );
			wp_enqueue_script( 'wp-chosen', self::$wp_chosen_url . '/wp-chosen.js', array( 'chosen', 'jquery' ), false, true );
		}
	}


	/**
	 * Checks the post content to see if it contains a select element. 
	 */
	private static function contains_select( $content = '' ){

		global $post;

		if ( empty( $content ) && is_object( $post ) ) {
			$content = $post->post_content;
		}
		// Contains a vanilla select element
		if( strpos( $content, '<select' ) !== false ) {
			return true;
		} elseif( strpos( $content, '[contact-form' ) !== false ) { // Contains Grunion Contact Form
			return true;
		} else {
			return false;
		}
	}

	/**
	 * Return an empty string in place of the [chosen] shortcode. It's simply a flag to 
	 * enqueue the appropriate scripts & styles.
	 */
	public static function shortcode_handler( $atts, $content = null ) {

		wp_enqueue_style(  'chosen', self::$chosen_url . '/chosen.css' );
		wp_enqueue_script( 'chosen', self::$chosen_url . '/chosen.jquery.min.js', array( 'jquery' ), false, true );
		wp_enqueue_script( 'wp-chosen', self::$wp_chosen_url . '/wp-chosen.js', array( 'chosen', 'jquery' ), false, true );

		return '';
	}
}

(The plugin hasn't been updated in 5 years, I don't think you have to worry about losing your changes.)

#1307923
chosen-updt.JPG

Hi Nigel,
Really appreciate you taking the time to investigate this and trying to solve the problem.
I copy / pasted your code into chosen.php (after making a copy ) the screen shot is what I get at the top of the page and no change to <select> box

#1307945

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Sorry, I didn't paste in the <?php which you need at the start of the file, which would be present in the original but which it looks like you overwrote when pasting in my code sample.

The plugin file needs to start with the <?php to indicate that what follows is PHP and not HTML.

#1307947

Just compared codes seems yours is missing opening <?php,
just trying with that, will let you know

#1307967

My issue is resolved now. Thank you!