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
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?
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?
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?
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.)
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
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.