WordPress: add_meta_box error - php

I rewrite the WordPress example to create a meta box, using functions as variables:
$myplugin_add_meta_box = function() {
add_meta_box('myplugin_sectionid', 'Testing', $myplugin_meta_box_callback, 'page' );
};
add_action( 'add_meta_boxes', $myplugin_add_meta_box );
$myplugin_meta_box_callback = function( $post ) {
wp_nonce_field( 'myplugin_save_meta_box_data', 'myplugin_meta_box_nonce' );
$value = get_post_meta( $post->ID, '_my_meta_value_key', true );
echo '<label for="myplugin_new_field">';
_e( 'Description for this field', 'myplugin_textdomain' );
echo '</label> ';
echo '<input type="text" id="myplugin_new_field" name="myplugin_new_field" value="' . esc_attr( $value ) . '" size="25" />';
};
But this error appear:
Warning: call_user_func() expects parameter 1 to be a valid callback,
no array or string given in
/var/www/public/wp-admin/includes/template.php on line 1037
I believe the error has something to do with the use of function as a variable.

add_action( 'add_meta_boxes', 'myplugin_add_meta_box' );
function myplugin_add_meta_box() {
add_meta_box('myplugin_sectionid', 'Testing', 'myplugin_meta_box_callback', 'page' );
}
function myplugin_meta_box_callback ( $post ) {
wp_nonce_field( 'myplugin_save_meta_box_data', 'myplugin_meta_box_nonce' );
$value = get_post_meta( $post->ID, '_my_meta_value_key', true );
echo '<label for="myplugin_new_field">';
_e( 'Description for this field', 'myplugin_textdomain' );
echo '</label> ';
echo '<input type="text" id="myplugin_new_field" name="myplugin_new_field" value="' . esc_attr( $value ) . '" size="25" />';
};
Declare myplugin_meta_box_callback as a function, not as a variable.

Found the solution:
add_meta_box('myplugin_sectionid', 'Testing', __NAMESPACE__ . '\\myplugin_meta_box_callback', 'page' );

Related

How to convert the WordPress meta box Page Attributes->Page Template dropdown to radio buttons?

I would like to be able to change the Page Attribute "Templates" dropdown to radio buttons, to allow me to have corresponding thumbnails next to them.
It looks like this question was already asked here - Is it possible to make WordPress page attribute meta box select option display as images? - however no code was ever supplied and looking at the comments it appears that the user answered their own question?
I have already removed and replaced the Page Attributes meta box on functions.php, as per below:
add_action( 'add_meta_boxes', 'wpse44966_add_meta_box' );
function wpse44966_add_meta_box( $post_type ){
remove_meta_box(
'pageparentdiv',
'page',
'side');
add_meta_box(
'wpse44966-meta-box',
'page' == $post_type ? __('Page Style Templates') : __('Attributes'),
'wpse44966_meta_box_cb',
'page',
'side',
'low');
}
I am then able to call back the "Templates" dropdown in my own meta box - I have also included the page_template_dropdown function (renamed 'page_template_dropdown_show_it').
function page_template_dropdown_show_it( $default = '', $post_type = 'page' ) {
$templates = get_page_templates( null, $post_type );
ksort( $templates );
foreach ( array_keys( $templates ) as $template ) {
$selected = selected( $default, $templates[ $template ], false );
echo "\n\t<option value='" . esc_attr( $templates[ $template ] ) . "' $selected>" . esc_html( $template ) . '</option>';
}
}
function wpse44966_meta_box_cb( $post ){
echo 'Please select from the below';
if ( count( get_page_templates( $post ) ) > 0 && get_option( 'page_for_posts' ) != $post->ID ) :
$template = ! empty( $post->page_template ) ? $post->page_template : false; ?>
<p class="post-attributes-label-wrapper"><label class="post-attributes-label" for="page_template"><?php _e( 'Template' ); ?></label><?php do_action( 'page_attributes_meta_box_template', $template, $post ); ?></p>
<select name="page_template" id="page_template">
<?php $default_title = apply_filters( 'default_page_template_title', __( 'Default Template' ), 'meta-box' ); ?>
<option value="default"><?php echo esc_html( $default_title ); ?></option>
<?php page_template_dropdown_show_it( $template, $post->post_type ); ?>
</select>
<?php endif;
}
However, when I then amend both page_template_dropdown_show_it and wpse44966_meta_box_cb to show radio buttons, the changes are applied visually but nothing happens when you select them?
function page_template_dropdown_show_it( $default = '', $post_type = 'page' ) {
$templates = get_page_templates( null, $post_type );
ksort( $templates );
foreach ( array_keys( $templates ) as $template ) {
$checked = checked( $default, $templates[ $template ], false );
echo "\n\t<input type='radio' name='page_template' value='" . esc_attr( $templates[ $template ] ) . "' $checked>" . esc_html( $template );
}
}
function wpse44966_meta_box_cb( $post ){
echo 'Please select from the below';
if ( count( get_page_templates( $post ) ) > 0 && get_option( 'page_for_posts' ) != $post->ID ) :
$template = ! empty( $post->page_template ) ? $post->page_template : false; ?>
<p class="post-attributes-label-wrapper"><label class="post-attributes-label" for="page_template"><?php _e( 'Template' ); ?></label><?php do_action( 'page_attributes_meta_box_template', $template, $post ); ?></p>
<?php $default_title = apply_filters( 'default_page_template_title', __( 'Default Template' ), 'meta-box' ); ?>
<input type='radio' name='page_template' value="default"><?php echo esc_html( $default_title ); ?>
<?php page_template_dropdown_show_it( $template, $post->post_type ); ?>
<?php endif;
}
It's obviously not as simple as a straight swap-out (as it isn't working), but the only thing that I can see that is missing is now there is nothing carrying the id="page_template", whereas before it was included in the below:
<select name="page_template" id="page_template">
So, it appears that I was correct that the issue was that the id "page_template" was no longer being passed.
To solve this, all I have done is applied some Javascript (using the admin_enqueue_scripts function) which adds the id "page_template" to the radio button that has been selected.

Wordpress plugin - options page not found

I was coding my very first plugin in order to share the posts on Facebook and Instagram, and I was writing the plugin options page.
I get always the error "Options page not found".
I thought that register_setting in the callback function could make the trick, but it didn't.
What am I doing wrong?
Here my code:
<?php
class Socialize {
public function __construct() {
// Hook into the admin menu
add_action( 'admin_menu', array( $this, 'create_plugin_settings_page' ) );
add_action( 'admin_init', array( $this, 'setup_sections' ) );
add_action( 'admin_init', array( $this, 'setup_fields' ) );
}
public function setup_fields() {
add_settings_field( 'facebook_account', 'Facebook Username', array( $this, 'fb_account_callback' ), 'smashing_fields', 'facebook_section' );
add_settings_field( 'facebook_password', 'Facebook Password', array( $this, 'fb_pwd_callback' ), 'smashing_fields', 'facebook_section' );
add_settings_field( 'instagram_account', 'Instagram Username', array( $this, 'insta_account_callback' ), 'smashing_fields', 'instagram_section' );
add_settings_field( 'instagram_password', 'Instagram Password', array( $this, 'insta_pwd_callback' ), 'smashing_fields', 'instagram_section' );
}
public function setup_sections() {
add_settings_section( 'facebook_section', 'Facebook Account', array( $this, 'section_callback' ), 'smashing_fields' );
add_settings_section( 'instagram_section', 'Instagram Account', array( $this, 'section_callback' ), 'smashing_fields' );
}
public function fb_account_callback( $arguments ) {
echo '<input name="fb_account" id="fb_account" type="text" value="' . get_option( 'facebook_account' ) . '" />';
register_setting( 'smashing_fields', 'facebook_account' );
}
public function fb_pwd_callback( $arguments ) {
echo '<input name="fb_pwd" id="fb_pwd" type="password" value="' . get_option( 'facebook_password' ) . '" />';
register_setting( 'smashing_fields', 'facebook_password' );
}
public function insta_account_callback( $arguments ) {
echo '<input name="insta_account" id="insta_account" type="text" value="' . get_option( 'instagram_account' ) . '" />';
register_setting( 'smashing_fields', 'instagram_account' );
}
public function insta_pwd_callback( $arguments ) {
echo '<input name="insta_pwd" id="insta_pwd" type="password" value="' . get_option( 'instagram_password' ) . '" />';
register_setting( 'smashing_fields', 'instagram_password' );
}
public function section_callback( $arguments ) {
switch ( $arguments['id'] ) {
case 'facebook_section':
echo 'This is the Facebook section';
break;
case 'instagram_section':
echo 'This is the Instagram section';
break;
}
}
public function create_plugin_settings_page() {
// Add the menu item and page
$page_title = 'Free Socialize';
$menu_title = 'Free Socialize';
$capability = 'manage_options';
$slug = 'smashing_fields';
$callback = array( $this, 'plugin_settings_page_content' );
$icon = 'dashicons-admin-plugins';
$position = 100;
add_menu_page( $page_title, $menu_title, $capability, $slug, $callback, $icon, $position );
}
public function plugin_settings_page_content() { ?>
<div class="wrap">
<h2>Free Socialize Settings Page</h2>
<form method="post" action="options.php">
<?php
settings_fields( 'smashing_fields' );
do_settings_sections( 'smashing_fields' );
submit_button();
?>
</form>
</div> <?php
}
}
new Socialize();
?>
I can't comment to gain clarification so apologies if I've misunderstood but would you not need to use add_plugins_page() in this instance?
<?php
add_plugins_page( $page_title, $menu_title, $capability, $menu_slug, $function);
?>
Docs: https://codex.wordpress.org/Function_Reference/add_plugins_page
Hope that helps and I've understood :)

Save data from multiple checkbox metaboxes in Wordpress

I'm learning how to add metaboxes to posts. I would like to create a group of metaboxes with text inputs and multiple checboxes. For now the checkboxes are just put there like that, but eventually they will be generated by a foreach loop with content from another place, so it's important for me to give them names like entry[0], entry[1] and so on. They have to be saved by a loop as I will not know how many will be generated.
This is what I have so far:
// adding the metaboxes
function add_post_reference() {
add_meta_box('post-reference', 'Reference', 'referenceCallBack', 'languagecourses', 'side', 'high');
}
add_action('add_meta_boxes', 'add_post_reference');
// callback function
function referenceCallBack($post) {
wp_nonce_field( 'reference_meta_box', 'reference_nonce' );
$name_value = get_post_meta( $post->ID, '_post_reference_name', true );
$link_value = get_post_meta( $post->ID, '_post_reference_link', true );
trying to do the same as above with my checkboxes but I don't know what to put there:
$teachers_value = get_post_meta( $post->ID, 'what do I put here?', true ); // what do I put here?
Echoing the html structure now (the text inputs work (values get saved), I'm trying to figure out how to make the checkboxes save as well:
echo '<label for="reference-name">'. 'Reference Name' .'</label>';
echo '<input type="text" id="reference-name" name="post_reference_name" placeholder="Example" value="'.$name_value.'" size="25"/>';
echo '<p class="howto">'. 'Add the name of the reference' .'</p>';
echo '<label for="reference-link">'. 'Reference Link' .'</label>';
echo '<input type="text" id="reference-link" name="post_reference_link" placeholder="http://www.example.com/" value="'.$link_value.'" size="25"/>';
echo '<p class="howto">'. 'Add the link of the reference' .'</p>';
// my checkboxes
echo '<input type="checkbox" name="entry[0]" value="moredata">';
echo '<input type="checkbox" name="entry[1]" value="moredata">';
echo '<input type="checkbox" name="entry[2]" value="moredata">';
echo '<input type="checkbox" name="entry[3]" value="moredata">';
echo '<input type="checkbox" name="entry[4]" value="moredata">';
}
function save_post_reference( $post_id ) {
if ( ! current_user_can( 'edit_post', $post_id ) ) {
return;
}
if ( ! isset( $_POST['reference_nonce'] ) ) {
return;
}
if ( ! wp_verify_nonce( $_POST['reference_nonce'], 'reference_meta_box' ) ) {
return;
}
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return;
}
if ( ! isset( $_POST['post_reference_name'] ) || ! isset( $_POST['post_reference_link'] ) ) {
return;
}
$reference_name = sanitize_text_field( $_POST['post_reference_name'] );
$reference_link = sanitize_text_field( $_POST['post_reference_link'] );
// looping through the checkboxes
for ($i = 0; $i < 5; $i++) {
$teachers_names = sanitize_text_field($_POST['entry'][$i]);
}
update_post_meta( $post_id, '_post_reference_name', $reference_name );
update_post_meta( $post_id, '_post_reference_link', $reference_link );
Again, what do I put here?
update_post_meta( $post_id, 'whatdoIputhere?', $teachers_names); // what do I put here?
}
add_action( 'save_post', 'save_post_reference' );
Could anybody please help me on that?
Your HTML code should be like :
echo '<input type="checkbox" name="entry[]" value="moredata">';
echo '<input type="checkbox" name="entry[]" value="moredata">';
echo '<input type="checkbox" name="entry[]" value="moredata">';
echo '<input type="checkbox" name="entry[]" value="moredata">';
echo '<input type="checkbox" name="entry[]" value="moredata">';
now you will save the data :
key = 'entry';
$values_to_save = array();
$new_values = $_POST['entry'];
$existing_values = get_post_meta( $post_id, $key, true ) ;
if(!empty($existing_values)){
foreach((array) $existing_values as $existing_value){
$values_to_save[] = $existing_value;
}
}
if(!empty($new_values)){
foreach((array) $new_values as $new_value ){
$values_to_save[] = $new_value ;
}
}
update_post_meta( $post_id, $key, $values_to_save );
Now to fetch the data use the below code :
$key = 'entry';
$values = get_post_meta( $post_id, $key, true );
foreach((array) $values as $value){
echo $value . '<br>';
}
Ok, I updated my functions with your code and this is how it looks now:
function add_post_reference() {
add_meta_box('post-reference', 'Reference', 'referenceCallBack', 'languagecourses', 'side', 'high');
}
add_action('add_meta_boxes', 'add_post_reference');
// callback
function referenceCallBack($post) {
wp_nonce_field( 'reference_meta_box', 'reference_nonce' );
$name_value = get_post_meta( $post->ID, '_post_reference_name', true );
$link_value = get_post_meta( $post->ID, '_post_reference_link', true );
$key = 'entry';
$values = get_post_meta( $post_id, $key, true );
foreach((array) $values as $value){
echo $value . '<br>';
}
echo '<label for="reference-name">'. 'Reference Name' .'</label>';
echo '<input type="text" id="reference-name" name="post_reference_name" placeholder="Example" value="'.$name_value.'" size="25"/>';
echo '<p class="howto">'. 'Add the name of the reference' .'</p>';
echo '<label for="reference-link">'. 'Reference Link' .'</label>';
echo '<input type="text" id="reference-link" name="post_reference_link" placeholder="http://www.example.com/" value="'.$link_value.'" size="25"/>';
echo '<p class="howto">'. 'Add the link of the reference' .'</p>';
echo '<input type="checkbox" name="entry[]" value="moredata">';
echo '<input type="checkbox" name="entry[]" value="moredata">';
echo '<input type="checkbox" name="entry[]" value="moredata">';
echo '<input type="checkbox" name="entry[]" value="moredata">';
echo '<input type="checkbox" name="entry[]" value="moredata">';
}
function save_post_reference( $post_id ) {
if ( ! current_user_can( 'edit_post', $post_id ) ) {
return;
}
if ( ! isset( $_POST['reference_nonce'] ) ) {
return;
}
if ( ! wp_verify_nonce( $_POST['reference_nonce'], 'reference_meta_box' ) ) {
return;
}
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return;
}
if ( ! isset( $_POST['post_reference_name'] ) || ! isset( $_POST['post_reference_link'] ) ) {
return;
}
$reference_name = sanitize_text_field( $_POST['post_reference_name'] );
$reference_link = sanitize_text_field( $_POST['post_reference_link'] );
$key = 'entry';
$values_to_save = array();
$new_values = $_POST['entry'];
$existing_values = get_post_meta( $post_id, $key, true ) ;
if(!empty($existing_values)){
foreach((array) $existing_values as $existing_value){
$values_to_save[] = $existing_value;
}
}
if(!empty($new_values)){
foreach((array) $new_values as $new_value ){
$values_to_save[] = $new_value ;
}
}
update_post_meta( $post_id, $key, $values_to_save );
update_post_meta( $post_id, '_post_reference_name', $reference_name );
update_post_meta( $post_id, '_post_reference_link', $reference_link );
}
add_action( 'save_post', 'save_post_reference' );
One thing I change was that key = to $key = as otherwise it was throwing an error.
And still - no change... one thing I thought about is that maybe it does save the data, but the checkboxes remain unchecked?

How to rearrange/customize html in woocommerce checkout form fields

I want to accomplish 2 things in my woocommerce checkout form:
1. Put some text between some groups of fields, for example (h3):
<h3>my custom heading</h3>
<p class="form-row form-row validate-required">
<input type="email">...
</p>
<h3>my other custom heading</h3>
<p class="form-row form-row validate-required">
<input type="text">...
</p>
<p class="form-row form-row validate-required">
<input type="text">...
</p>
2. Display input tag first and label tag as second in html (woocommerce display label before input by default)
I figured out that checkout form is displayed by this code (for billing):
<?php foreach ( $checkout->checkout_fields['billing'] as $key => $field ) : ?>
<?php woocommerce_form_field( $key, $field, $checkout->get_value( $key ) ); ?>
<?php endforeach; ?>
How can I customize it in way I described?
I'm not sure on part 1, but for part 2, you can modify the output of woocommerce_form_field() by filtering woocommerce_form_field_$type.
So your part 2 can be solved like so:
function so_39267627_form_field( $field, $key, $args, $value ){
if ( $args['required'] ) {
$args['class'][] = 'validate-required';
$required = ' <abbr class="required" title="' . esc_attr__( 'required', 'woocommerce' ) . '">*</abbr>';
} else {
$required = '';
}
$args['maxlength'] = ( $args['maxlength'] ) ? 'maxlength="' . absint( $args['maxlength'] ) . '"' : '';
$args['autocomplete'] = ( $args['autocomplete'] ) ? 'autocomplete="' . esc_attr( $args['autocomplete'] ) . '"' : '';
if ( is_string( $args['label_class'] ) ) {
$args['label_class'] = array( $args['label_class'] );
}
if ( is_null( $value ) ) {
$value = $args['default'];
}
// Custom attribute handling
$custom_attributes = array();
// Custom attribute handling
$custom_attributes = array();
if ( ! empty( $args['custom_attributes'] ) && is_array( $args['custom_attributes'] ) ) {
foreach ( $args['custom_attributes'] as $attribute => $attribute_value ) {
$custom_attributes[] = esc_attr( $attribute ) . '="' . esc_attr( $attribute_value ) . '"';
}
}
$field = '';
$label_id = $args['id'];
$field_container = '<p class="form-row %1$s" id="%2$s">%3$s</p>';
$field .= '<input type="' . esc_attr( $args['type'] ) . '" class="input-text ' . esc_attr( implode( ' ', $args['input_class'] ) ) .'" name="' . esc_attr( $key ) . '" id="' . esc_attr( $args['id'] ) . '" placeholder="' . esc_attr( $args['placeholder'] ) . '" ' . $args['maxlength'] . ' ' . $args['autocomplete'] . ' value="' . esc_attr( $value ) . '" ' . implode( ' ', $custom_attributes ) . ' />';
if ( ! empty( $field ) ) {
$field_html = '';
$field_html .= $field;
if ( $args['description'] ) {
$field_html .= '<span class="description">' . esc_html( $args['description'] ) . '</span>';
}
if ( $args['label'] && 'checkbox' != $args['type'] ) {
$field_html .= '<label for="' . esc_attr( $label_id ) . '" class="' . esc_attr( implode( ' ', $args['label_class'] ) ) .'">' . $args['label'] . $required . '</label>';
}
$container_class = 'form-row ' . esc_attr( implode( ' ', $args['class'] ) );
$container_id = esc_attr( $args['id'] ) . '_field';
$after = ! empty( $args['clear'] ) ? '<div class="clear"></div>' : '';
$field = sprintf( $field_container, $container_class, $container_id, $field_html ) . $after;
}
return $field;
}
add_filter( 'woocommerce_form_field_password', 'so_39267627_form_field', 10, 4 );
add_filter( 'woocommerce_form_field_text', 'so_39267627_form_field', 10, 4 );
add_filter( 'woocommerce_form_field_email', 'so_39267627_form_field', 10, 4 );
add_filter( 'woocommerce_form_field_tel', 'so_39267627_form_field', 10, 4 );
add_filter( 'woocommerce_form_field_number', 'so_39267627_form_field', 10, 4 );
You would need to write a few more functions (mostly I copied and pasted whole swaths of code from WooCommerce and then just swapped the label part around ) for other other field types, but this should serve as an example.
For the first one you can do the following to display single input field
<?php
woocommerce_form_field(
"billing_first_name",
$checkout->checkout_fields['billing']['billing_first_name'],
$checkout->get_value( 'billing_first_name' )
);
?>
where you can replace first_name with any key of the checkout billing fields
So for your example it will be something like this
<h3>my custom heading</h3>
<p class="form-row form-row validate-required">
<?php woocommerce_form_field( "billing_email", $checkout->checkout_fields['billing']['billing_email'], $checkout->get_value( 'billing_email' ) ); ?>
</p>
As for the second I'm not sure how you can achieve that. I needed something similar and i used javascript to reposition the labels.
something like :
jQuery('form.checkout label').each(function(){
jQuery(this).insertAfter(jQuery(this).parent().find('input'));
})
As far as I think, woo commerce does not support html, there's a plugin known as woocommerce checkout field editor, see if it solves your issue.

Adding caption area to 'Flexslider Plug-in'

I have this question on the Wordpress stack exchange as well, but not having any luck there. So, as my solution probably involves me hard-coding php and css, It may be better to have it here.
I'm using 'Flex Slider' plugin - that works on top of 'WP rotator' plug-in on my Wordpress 3.2 website. I have it implemented fine, and beginning to look at inserting my content - but I need to add a caption to be on top of the slider. As are present on most sliders on the web, within the documentation of the non-Wordpress plugin of the tool it suggests I can do something like;
<div class="flex-container">
<div class="flexslider">
<ul class="slides">
<li>
<img src="slide1.jpg" />
<p class="flex-caption">Captions and cupcakes. Winning combination.</p>
</li>
<li>
<img src="slide2.jpg" />
<p class="flex-caption">This image is wrapped in a link!</p>
</li>
<li>
<img src="slide3.jpg" />
</li>
</ul>
</div>
</div>
Problem is; with the Wordpress plug-in version, I can't find that markup to work inside.
Here's the only non-css non-js file in the plug-ins directory, so I assume I have to work in there.
I've tried inserting the mark-up that was suggested non-Wordpress above, but not sure where to insert it as it's broke it with my attempts thus far.
<?php
/*
Plugin Name: Flex Slider for WP Rotator
Plugin URI: http://wordpress.org/extend/plugins/flex-slider-for-wp-rotator/
Description: Turns WP Rotator into FlexSlider, a fully responsive jQuery slider.
Version: 1.1
Author: Bill Erickson
Author URI: http://www.billerickson.net/blog/wordpress-guide
*/
class BE_Flex_Slider {
var $instance;
function __construct() {
$this->instance =& $this;
register_activation_hook( __FILE__, array( $this, 'activation_hook' ) );
add_action( 'plugins_loaded', array( $this, 'init' ) );
}
/**
* Activation Hook
* Confirm WP Rotator is currently active
*/
function activation_hook() {
if( !function_exists( 'wp_rotator_option' ) ) {
deactivate_plugins( plugin_basename( __FILE__ ) );
wp_die( sprintf( __( 'Sorry, you can’t activate unless you have installed WP Rotator', 'flex-slider-for-wp-rotator'), 'http://wordpress.org/extend/plugins/wp-rotator/' ) );
}
}
function init() {
// Remove original scripts and styles
remove_action('wp_head','wp_rotator_css');
remove_action('admin_head','wp_rotator_css');
remove_action('wp_head','wp_rotator_javascript');
remove_action('admin_head','wp_rotator_javascript');
remove_action('init','wp_rotator_add_jquery');
remove_action('admin_init','wp_rotator_add_jquery');
// Enqueue Scripts and Styles
add_action( 'init', array( $this, 'enqueue_scripts_and_styles' ) );
// Remove original outer markup
remove_action( 'wp_rotator', 'wp_rotator' );
// Add new markup
add_action( 'wp_rotator', array( $this, 'flex_slider' ) );
remove_shortcode( 'wp_rotator' );
add_shortcode( 'wp_rotator', array( $this, 'flex_slider_markup' ) );
}
function enqueue_scripts_and_styles() {
// Use this filter to limit where the scripts are enqueued.
$show = apply_filters( 'be_flex_slider_show_scripts', true );
if ( true === $show ) {
wp_enqueue_style( 'flex-slider', plugins_url( 'flexslider.css', __FILE__ ) );
wp_enqueue_script( 'jquery ');
wp_enqueue_script( 'flex-slider', plugins_url( 'jquery.flexslider-min.js', __FILE__ ), array( 'jquery' ) );
add_action( 'wp_head', array( $this, 'flex_slider_settings' ) );
}
}
function flex_slider_settings() {
?>
<script type="text/javascript" charset="utf-8">
jQuery(window).load(function() {
jQuery('.flexslider').flexslider({
<?php
$flex_settings = array(
'animation' => '"' . wp_rotator_option( 'animate_style' ) . '"',
'slideshowSpeed' => wp_rotator_option( 'rest_ms' ),
'animationDuration' => wp_rotator_option( 'animate_ms' ),
);
$flex_slide_settings = array(
'controlsContainer' => '".flex-container"'
);
if( 'slide' == wp_rotator_option( 'animate_style' ) )
$flex_settings = array_merge( $flex_settings, $flex_slide_settings );
$flex_settings = apply_filters( 'be_flex_slider_settings', $flex_settings );
foreach ( $flex_settings as $field => $value ) {
echo $field . ': ' . $value . ', ';
}
?>
});
});
</script>
<?php
}
function flex_slider_markup() {
$output = '';
if( 'slide' == wp_rotator_option( 'animate_style' ) )
$output .= '<div class="flex-container">';
$output .= '<div class="flexslider"><ul class="slides">';
$loop = new WP_Query( esc_attr( wp_rotator_option('query_vars') ) );
while ( $loop->have_posts() ): $loop->the_post(); global $post;
$url = esc_url ( get_post_meta( $post->ID, 'wp_rotator_url', true ) );
if ( empty( $url ) ) $url = get_permalink($post->ID);
$show_info = esc_attr( get_post_meta( $post->ID, 'wp_rotator_show_info', true ) );
if ( true == $show_info ) {
$title = get_the_title();
if ( get_the_excerpt() ) $excerpt = get_the_excerpt();
else $excerpt = '';
$caption = $title . ' <span class="excerpt">' . $excerpt . '</span>';
$info = '<p class="flex-caption">' . apply_filters( 'be_flex_slider_caption', $caption, $title, $excerpt ) . '</p>';
} else {
$info = '';
}
$image = wp_get_attachment_image_src( get_post_thumbnail_id(), 'wp_rotator' );
$slide = '<li><img src="' . $image[0] . '" />' . $info . '</li>';
$output .= apply_filters( 'be_flex_slider_slide', $slide );
endwhile; wp_reset_query();
$output .= '</ul></div>';
if( 'slide' == wp_rotator_option( 'animate_style' ) )
$output .= '</div>';
return $output;
}
function flex_slider() {
echo $this->flex_slider_markup();
}
}
new BE_Flex_Slider;
?>
I have contacted the plug-in developer, he's not responding so I assume hes not going to support my question - so I'm left to handcode.
http://wordpress.org/extend/plugins/wp-rotator/
http://flex.madebymufffin.com/
http://wordpress.org/extend/plugins/flex-slider-for-wp-rotator/
Thanks for any pointers!
It looks like captions are automatically added to the slider as long as you set the post to show rotator info (wp_rotator_show_info... probably on the plugin settings page or on your individual post page). The automatic caption is made up of the title of the post plus the excerpt. Here's the key part in the plugin above:
$show_info = esc_attr( get_post_meta( $post->ID, 'wp_rotator_show_info', true ) );
if ( true == $show_info ) {
$title = get_the_title();
if ( get_the_excerpt() ) $excerpt = get_the_excerpt();
else $excerpt = '';
$caption = $title . ' <span class="excerpt">' . $excerpt . '</span>';
$info = '<p class="flex-caption">' . apply_filters( 'be_flex_slider_caption', $caption, $title, $excerpt ) . '</p>';
} else {
$info = '';
}
UPDATE: If you want the caption to show no matter what, replace the above portion with this:
$title = get_the_title();
if ( get_the_excerpt() ) $excerpt = get_the_excerpt();
else $excerpt = '';
$caption = $title . ' <span class="excerpt">' . $excerpt . '</span>';
$info = '<p class="flex-caption">' . apply_filters( 'be_flex_slider_caption', $caption, $title, $excerpt ) . '</p>';
Note that I merely deleted the part that checks for wp_rotator_show_info.

Categories