Add custom style formats to TinyMCE (Advanced Custom Fields) - php

I have created a new TinyMCE layout for Advanced Custom Fields called "Very Simple" that I want to use on specific WYSIWYG fields. I've managed to add the buttons I want and I'm now looking for a way to add a list of custom style formats as a dropdown, but I can't really figure out how to do this.
The code I have right now is the following:
// Customize ACF MCE
add_filter( 'acf/fields/wysiwyg/toolbars' , 'new_toolbars' );
function new_toolbars( $toolbars )
{
// - this toolbar has only 1 row of buttons
$toolbars['Very Simple' ] = array();
$toolbars['Very Simple' ][1] = array('bold' , 'italic' , 'underline', 'link', 'unlink' );
$style_formats = array(
// These are the custom styles
array(
'title' => 'Red Button',
'block' => 'span',
'classes' => 'red-button',
'wrapper' => true,
),
array(
'title' => 'Content Block',
'block' => 'span',
'classes' => 'content-block',
'wrapper' => true,
),
array(
'title' => 'Highlighter',
'block' => 'span',
'classes' => 'highlighter',
'wrapper' => true,
),
);
// Insert the array, JSON ENCODED, into 'style_formats'
$toolbars['Very Simple'][1]['styleselect'] = json_encode( $style_formats );
// return $toolbars - IMPORTANT!
return $toolbars;
}
The style format dropdown is not showing. Any ideas why?

The styleselect dropdown is hidden by default, so to get any registered formats/styles will show, you'll just need to activate the styleselect dropdown menu in the Visual editor.
The below function (from the WordPress Codex) filters the array of buttons loaded by TinyMCE, so just add this to your functions.php. I'm using the mce_buttons_2 filter to make it appear in the second row of my toolbar.
// Callback function to insert 'styleselect' into the $buttons array
function my_mce_buttons_2( $buttons ) {
array_unshift( $buttons, 'styleselect' );
return $buttons;
}
// Register our callback to the appropriate filter
add_filter( 'mce_buttons_2', 'my_mce_buttons_2' );

Related

Add custom font icons to visual composer

I'm adding new icons to the Visual Composer iconbox in wordpress but i get the following 2 errors anyone know how to fix? Below is the code in my functions.php file
// Add new custom font to Font Family selection in icon box module
function myprefix_add_new_icon_set_to_iconbox( ) {
$param = WPBMap::getParam( 'vcex_icon_box', 'icon_type' );
$param['value'][__( 'CUSTOM ICONS NAME', 'total' )] = 'my_custom_icons';
vc_update_shortcode_param( 'vcex_icon_box', $param );
}
add_filter( 'init', 'myprefix_add_new_icon_set_to_iconbox', 40 );
// Add font picker setting to icon box module when you select your font family from the dropdown
function myprefix_add_font_picker() {
vc_add_param( 'vcex_icon_box', array(
'type' => 'iconpicker',
'heading' => esc_html__( 'Icon', 'total' ),
'param_name' => 'my_custom_icons',
'settings' => array(
'emptyIcon' => true,
'type' => 'my_custom_icons',
'iconsPerPage' => 20,
),
'dependency' => array(
'element' => 'icon_type',
'value' => 'my_custom_icons',
),
'group' => esc_html__( 'Icon', 'total' ),
)
);
}
add_filter( 'vc_after_init', 'myprefix_add_font_picker', 40 );
// Add array of your fonts so they can be displayed in the font selector
function my_icon_array() {
return array(
array(
'bg-icon-twitter' => 'Twitter',
'bg-icon-user' => 'User'
));
}
add_filter( 'vc_iconpicker-type-my_custom_icons', 'my_icon_array' );
Notice:
Wrong name for shortcode:vcex_icon_box. Name required in
/home/.../plugins/js_composer/include/classes/core/class-wpb-map.php
on line 472
Warning:
Cannot use a scalar value as an array in
/home/.../plugins/js_composer/include/classes/core/class-wpb-map.php
on line 367
Error 1 is caused by the fact you don't have a shortcode in your installation called "vcex_icon_box". Try "vc_icon" instead.
Also, if you use vc_icon, you will need to change the dependency element to type and not icon_type.
For error 2, WPBMap::getParam( 'vcex_icon_box', 'icon_type' ); is returning a scalar value, which you can then treat it like an array.
As a debug tip, its a good idea to test the outputs of functions so you understand what you are getting.
The VC documentation is also not the greatest.

Populate ACF Checkboxes with Post Types

I'm trying to populate a checkbox ACF field with the various post types of a WP site. This is for a plugin, so the post types will vary based on the location of install.
By default, the plugin uses pages and posts as it's post types, but need to give user option to use checkboxes to select other CPT's on the site. How can I populate the checkbox field with the list of all CPT's on a site. Here is my current section of PHP code to load the field within the plugin
array (
'key' => 'field_56e6d87b6c7be',
'label' => 'Add to Custom Post Types',
'name' => 'fb_pixel_cpt_select',
'type' => 'checkbox',
'instructions' => 'Select which Custom Post Types you would like to use.',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array (
'width' => '',
'class' => '',
'id' => '',
),
'choices' => array (
),
'default_value' => array (
),
'layout' => 'vertical',
'toggle' => 0,
),
You could use the ACF load field function here to auto populate your field. See more here: http://www.advancedcustomfields.com/resources/acfload_field/
Then, using the Wordpress get_post_types call (https://codex.wordpress.org/Function_Reference/get_post_types) you could retrieve those values and populate your field like this.
add_filter('acf/load_field/name=fb_pixel_cpt_select', 'acf_load_post_types');
function acf_load_post_types($field)
{
foreach ( get_post_types( '', 'names' ) as $post_type ) {
$field['choices'][$post_type] = $post_type;
}
// return the field
return $field;
}
Make sure that your field is already created before you try to populate it though.
This will create a select field for your ACF block or whatever that has all the post types except those that don't have a nav menu (such as the attachment post type).
add_filter('acf/load_field/name=select_post_type', 'yourprefix_acf_load_post_types');
/*
* Load Select Field `select_post_type` populated with the value and labels of the singular
* name of all public post types
*/
function yourprefix_acf_load_post_types( $field ) {
$choices = get_post_types( array( 'show_in_nav_menus' => true ), 'objects' );
foreach ( $choices as $post_type ) :
$field['choices'][$post_type->name] = $post_type->labels->singular_name;
endforeach;
return $field;
}
When I need to do this (choose from list of post types) I would be creating a list of posts based on post-type to feed into WP Query. So I use a select field in ACF, and add the post type names into the select options when creating the field, e.g.
posts : Posts
which I then use in the arguments for the WP Query, like so:
$theselectfield = get_field('theselectfield');
Then use it in the arguement of the query:
$args = array(
'post_type' => $theselectfield,
'some_other' => 'argument',
);

How to display related data in dropdown list by selecting specific data from corresponding dropdown list

I am using Drupal7,in that i have module where in i have written my own functions using php,with two dropdown list in row, now my requirement is when i select for example say "A" from 1st drop down then in 2nd drop down only related to "A" all data need show in the second dropdown,
In simple words if i select the city name from the 1st dropdown, then all the colleges of that city should display in 2nd dropdown.How can i achieve this in php?i found this link and piece of code http://w3shaman.com/article/creating-ajax-dropdown-drupal-7 but no use, it will be great helpful for me if someone answer for this.
If you created your form using Drupal Form API, then you can achive this using the Drupal Ajax.
Below is a code snippet from the Drupal Ajax examples module
function ajax_example_dependent_dropdown($form, &$form_state) {
// Get the list of options to populate the first dropdown.
$options_first = _ajax_example_get_first_dropdown_options();
// If we have a value for the first dropdown from $form_state['values'] we use
// this both as the default value for the first dropdown and also as a
// parameter to pass to the function that retrieves the options for the
// second dropdown.
$selected = isset($form_state['values']['dropdown_first']) ? $form_state['values']['dropdown_first'] : key($options_first);
$form['dropdown_first'] = array(
'#type' => 'select',
'#title' => 'Instrument Type',
'#options' => $options_first,
'#default_value' => $selected,
// Bind an ajax callback to the change event (which is the default for the
// select form type) of the first dropdown. It will replace the second
// dropdown when rebuilt.
'#ajax' => array(
// When 'event' occurs, Drupal will perform an ajax request in the
// background. Usually the default value is sufficient (eg. change for
// select elements), but valid values include any jQuery event,
// most notably 'mousedown', 'blur', and 'submit'.
// 'event' => 'change',
'callback' => 'ajax_example_dependent_dropdown_callback',
'wrapper' => 'dropdown-second-replace',
),
);
$form['dropdown_second'] = array(
'#type' => 'select',
'#title' => $options_first[$selected] . ' ' . t('Instruments'),
// The entire enclosing div created here gets replaced when dropdown_first
// is changed.
'#prefix' => '<div id="dropdown-second-replace">',
'#suffix' => '</div>',
// When the form is rebuilt during ajax processing, the $selected variable
// will now have the new value and so the options will change.
'#options' => _ajax_example_get_second_dropdown_options($selected),
'#default_value' => isset($form_state['values']['dropdown_second']) ? $form_state['values']['dropdown_second'] : '',
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Submit'),
);
return $form;
}
function ajax_example_dependent_dropdown_callback($form, $form_state) {
return $form['dropdown_second'];
}
function _ajax_example_get_second_dropdown_options($key = '') {
$options = array(
t('String') => drupal_map_assoc(
array(
t('Violin'),
t('Viola'),
t('Cello'),
t('Double Bass'),
)
),
t('Woodwind') => drupal_map_assoc(
array(
t('Flute'),
t('Clarinet'),
t('Oboe'),
t('Bassoon'),
)
),
t('Brass') => drupal_map_assoc(
array(
t('Trumpet'),
t('Trombone'),
t('French Horn'),
t('Euphonium'),
)
),
t('Percussion') => drupal_map_assoc(
array(
t('Bass Drum'),
t('Timpani'),
t('Snare Drum'),
t('Tambourine'),
)
),
);
if (isset($options[$key])) {
return $options[$key];
}
else {
return array();
}
}

Adding column to Wordpress Post (Specifically WP E-Commerce)

Right, I've got WordPress E-commerce installed on WordPress and I need to add additional columns to the post type.
I've done some investigating. It appears that E-commerce just submits a post type called "Products" and changes the columns in order to add things like Price etc.
I need to add another input. Just a little checkbox that the admin can set to true or false as they add a product. The only problem for me at the moment is finding where exactly to do this.
I think I've found the WordPress E-Commerce post type column settings, but obviously just adding an additional one isn't working.
/wp-content/plugins/wp-e-commerce/wpsc-admin/display-items.page.php
function wpsc_additional_column_names( $columns ){
$columns = array();
$columns['cb'] = '';
$columns['image'] = '';
$columns['title'] = __('Name', 'wpsc');
$columns['stock'] = __('Stock', 'wpsc');
$columns['price'] = __('Price', 'wpsc');
$columns['sale_price'] = __('Sale', 'wpsc');
$columns['SKU'] = __('SKU', 'wpsc');
$columns['weight'] = __('Weight', 'wpsc');
$columns['cats'] = __('Categories', 'wpsc');
$columns['featured'] = '';
$columns['hidden_alerts'] = '';
$columns['date'] = __('Date', 'wpsc');
return $columns;
}
Don't edit the core files. You can add custom metaboxes to WP e-Commerce's Products post type just as you would any other post type.
My preferred solution is to use Custom Metaboxes and Fields for WordPress
This sample function will output a checkbox on products using the above plugin (note 'pages' => array('wpsc-product'), this targets products only):
function base_meta_boxes_ba($meta_boxes) {
/**
* Page Options meta box
*/
$meta_boxes[] = array(
'id' => 'product_options',
'title' => 'Extra Product Options',
'pages' => array('wpsc-product'),
'context' => 'normal',
'priority' => 'high',
'show_names' => true,
'fields' => array(
array(
'name' => 'Test Checkbox',
'desc' => 'field description (optional)',
'id' => $prefix . 'test_checkbox',
'type' => 'checkbox'
),
)
);
return $meta_boxes;
}
add_filter('cmb_meta_boxes', 'base_meta_boxes_ba');

Wordpress: add events inside TinyMCE iframe

I've added some div wrapper styles to Wordpress, but when I use them in the page, it is impossible to add content below them in the page. CSS:after works inside the divs to create a selectable area, but it doesn't work to create a selectable area after the div.
In my functions.php I have:
function my_mce_before_init( $settings ) {
$style_formats = array(
array(
'title' => 'Box Two Columns',
'block' => 'div',
'classes' => 'twocolbox',
'wrapper' => true
),
array(
'title' => 'Image with Caption',
'block' => 'div',
'classes' => 'img_caption',
'wrapper' => true
),
array(
'title' => 'Gallery Horizontal',
'block' => 'div',
'classes' => 'scroller horizontal',
'wrapper' => true
)
);
$settings['style_formats'] = json_encode( $style_formats );
return $settings;
}
add_filter('tiny_mce_before_init', 'my_mce_before_init');
add_editor_style();
Is there a way to use execCommand here to add some html after the div styles I defined? To add something like an empty paragraph tag afterwards with a clear float?
I tried:
tinyMCE.execCommand('mceInsertContent',false,'Hello world!!');"
MCE editor breaks then.
...
Tried this but it's too buggy:
In editor-styles.css:
#tinyMCE:after {
content: " ";
clear:both;
width: 4em; height:4em;
}
Note that you may need to clear cache AND shift-reload button to see any changes to editor-styles.css in Wordpress.
...
Still working on this. Found a thread:
To access tinymce iframe elements through jquery
I tried adding the code in this thread to my_mce_before_init, but it just broke.
....
Also tried loading a jQuery script, but the target paths wouldn't work on the TinyMCE iframe. None of these selectors work:
jQuery(document).ready(function($) {
$("#tinyMCE").find("div").after('<p style="width:100%; clear:both; height:1em;"> 6789</p>');
$("div").css("color","red");
$("#content_ifr").contents().find("div").after('<p style="width:100%; clear:both; height:1em;"> 6789</p>');
$("#content_ifr").contents().find("#tinymce p").css("color","red");
$("#wp-content-editor-container").find("textarea").css("color","red");
$("iframe").contents().find("p").css("color","red");
$('#content_ifr').load(function(){
$('#content_ifr').contents().find('p').css("color","red");
});
});
You can add html pseudo elements using the tinymce configuration option content_css.
There you can define after elements. Give it a try!
Update:
When initializing tinymce set the setup paramter to the following (inside tinyMCE.init({...})
...
theme: "advanced", // example param
setup : function(ed) {
ed.onInit.add(function(ed, evt) {
$("#content_ifr").contents().find("p").css("color","red");
// other approach
//$(ed.getBody()).find('p').css("color","red");
});
},
cleanup: true, // example param
...

Categories