SMOF transition to the Redux Framework - php

I try to make the transfer of a WordPress theme options from SMOF to the Redux Framework since SMOF is no longer maintained. I have successfully transferred all other options and they work perfectly but I have trouble in following context, getting this error:
Parse error: syntax error, unexpected 'foreach' (T_FOREACH), expecting ')'
The old code was used to create a built-in translator option using "type" => "text", and has works great but no I do not know how to implement it in the new Redux Framework to run and no longer occur this error.
Here's the old code from the SMOF Framework:
$translate_strings = theme_get_translate_options();
foreach ( $translate_strings as $string_key => $string ) {
$of_options[] = array( "name" => esc_html( $string['string_text'] ),
"id" => 'td_'.$string_key,
"type" => "text",
);
}
And here is the new interface of Redux Framework. where are supposed to implement the old code using : "type" => "text", :
$translate_strings = theme_get_translate_options();
// -> START Translation Info Fields
Redux::setSection( $opt_name, array(
'title' => __( 'Translator', 'redux-framework-demo' ),
'desc' => __( '', 'redux-framework-demo' ),
'id' => 'translator-info-subsection',
'subsection' => true,
'fields' => array(
array(
'id' => '',
'type' => 'text',
'title' => __( '', 'redux-framework-demo' ),
'desc' => __( '', 'redux-framework-demo' ),
),
)
) );
The code that I used and makes the errors occurs is the following:
$translate_strings = theme_get_translate_options();
Redux::setSection( $opt_name, array(
'title' => __( 'Translator', 'redux-framework-demo' ),
'desc' => __( '', 'redux-framework-demo' ),
'id' => 'translator-info-subsection',
'subsection' => true,
'fields' => array(
foreach ( $translate_strings as $string_key => $string ) {
array(
'id' => 'td_'.$string_key,
'type' => 'text',
'title' => __( '', 'redux-framework-demo' ),
),
}
)
) );
I'm completely stuck on this and any help or suggestion will be greatly appreciated.
Thank you!

A little reorganization should be enough:
$translate_strings = theme_get_translate_options();
$myFields = array();
foreach ( $translate_strings as $string_key => $string ) {
$myFields[]=
array(
'id' => 'td_'.$string_key,
'type' => 'text',
'title' => __( '', 'redux-framework-demo' ),
);
}
Redux::setSection( $opt_name, array(
'title' => __( 'Translator', 'redux-framework-demo' ),
'desc' => __( '', 'redux-framework-demo' ),
'id' => 'translator-info-subsection',
'subsection' => true,
'fields' => $myFields
) );
I took out your foreach from the initialization and initialized the array separately.

Related

Wordpress - Broken UI of CMB2/cmb2-attached-posts library

I have implemented the CMB2/cmb2-attached-post library in my wordpress installation and created a custom post type in which to use it.
I have implemented the example described in the example-field-setup-php, specifically, in this way:
$example_meta = new_cmb2_box( array(
'id' => 'cmb2_attached_posts_field',
'title' => __( 'Attached Posts', 'yourtextdomain' ),
'object_types' => array( 'luogo' ), // Post type
'context' => 'normal',
'priority' => 'high',
'show_names' => true, // Show field names on the left
) );
$example_meta->add_field( array(
'name' => __( 'Attached Posts', 'yourtextdomain' ),
'desc' => __( 'Drag ...', 'yourtextdomain' ),
'id' => 'attached_cmb2_attached_posts',
'type' => 'custom_attached_posts',
'column' => true,
'options' => array(
'show_thumbnails' => true,
'filter_boxes' => true,
'query_args' => array(
'posts_per_page' => 10,
'post_type' => 'page',
),
),
) );
$example_meta->add_field( array(
'name' => __( 'Attached Users', 'yourtextdomain' ),
'desc' => __( 'Drag ...', 'yourtextdomain' ),
'id' => 'attached_cmb2_attached_users',
'type' => 'custom_attached_posts',
'column' => true,
'options' => array(
'show_thumbnails' => true,
'filter_boxes' => true,
'query_users' => true,
),
) );
The result I get graphically is this:
Which is visibly broken and malfunctioning.
What I got as a result is the following: (as shown on the library repo).
Why do I get a broken result if the library has been integrated as per the documentation?
Is something missing?
What I have done for use is:
Installing the library with composer and the inclusion of the library in the .php file where I create the custom post type:
require_once ( get_template_directory() . /vendor/webdevstudios/cmb2-attached-posts/init.php' );

Adding Row settings to WPBakery plugin

How do I get the settings to the frontend? I need to add classes to vc_row.
/**
* Ken Burns Effect for Row.
*/
add_action( 'vc_after_init', 'ken_burns_effect_add_option_to_vc_row' );
function ken_burns_effect_add_option_to_vc_row() {
// Ken Burns Effect Attributes
$ken_burns_effect_attributes =
array(
array(
'type' => 'checkbox',
'heading' => __( 'Ken Burns for image background', 'ken_burns_effect' ),
'param_name' => 'enable_ken_burns_effect',
'value' => array(
__( 'Yes', 'ken_burns_effect' ) => 'yes',
),
'description' => 'Check this box if you want to enable ken burns effect for this row.',
),
array(
'type' => 'dropdown',
'heading' => __( 'Direction', 'ken_burns_effect' ),
'param_name' => 'direction_ken_burns_effect',
'value' => array(
'Zoom In' => 'zoom_in',
'Zoom Out' => 'zoom_out',
),
'description' => __( '', 'ken_burns_effect' ),
'dependency' => array(
'element' => 'enable_ken_burns_effect',
'value' => array( 'yes' ),
),
),
array(
'type' => 'textfield',
'heading' => __( 'Transition speed', 'ken_burns_effect' ),
'param_name' => 'transition_speed_ken_burns_effect',
'value' => '',
'description' => __( '', 'ken_burns_effect' ),
'dependency' => array(
'element' => 'enable_ken_burns_effect',
'value' => array( 'yes' ),
),
),
);
vc_add_params( 'vc_row', $ken_burns_effect_attributes);
}
You will have to override the vc_row.php by copying it to your theme or plugin.
You can check their documentation on how to set the override directory: https://kb.wpbakery.com/docs/inner-api/vc_set_shortcodes_templates_dir/
Then you can use your custom params inside vc_row.php using their param_name eg:
$enable_ken_burns_effect;
$direction_ken_burns_effect;
$transition_speed_ken_burns_effect;
Keep in mind that you have to override vc_row_inner.php too if you want to use options there. Same goes for section and columns.

How to programmatically add an ACF group to the backend of Wordpress?

I've tried a bunch of different functions and approaches but so far I haven't been able to get it working.
The goal is to add an Advanced Custom Field group to the backend of Wordpress with some PHP-code. In the best scenario we add the PHP-code to a method of a class.
public function create_group( $group_name ) {
if ( $this->does_group_already_exists( $group_name ) ) {
return false;
}
acf_add_local_field_group( array(
'key' => 'group_1',
'title' => 'My Group',
'fields' => array(
array(
'key' => 'field_1',
'label' => 'Sub Title',
'name' => 'sub_title',
'type' => 'text',
)
),
'location' => array(
array(
array(
'param' => 'post_type',
'operator' => '==',
'value' => 'post',
),
),
),
) );
return true;
}
Nothing gets added with the code above. I also tried adding it to functions.php and it with a add_action() function like so:
add_action( 'acf/init', array( $this, 'create_group' ) );
But again, no results.
Hope some one can share a working solution.
Today I finally discovered a solution for adding a ACF group to the backend dynamically with PHP-code.
It can be done by adding a new post directly with the acf-field-group post type. Here is my implementation for those awesome people from the future that are interested:
public function create_form( $form_name ) {
$new_post = array(
'post_title' => $form_name,
'post_excerpt' => sanitize_title( $form_name ),
'post_name' => 'group_' . uniqid(),
'post_date' => date( 'Y-m-d H:i:s' ),
'comment_status' => 'closed',
'post_status' => 'publish',
'post_type' => 'acf-field-group',
);
$post_id = wp_insert_post( $new_post );
return $post_id;
}
Where $form_name is the name of the ACF group. It works. And there was no need for using a specific hook. I could just call this method directly.
Actually you can create such code over ACF in the WP-Backend itself (not sure if this only works in ACF Pro). Under Admin -> Custom Fields -> Tools -> Export -> Create PHP. The generated code is a great starting point for a programmatic ACF integration.
It should look something like this:
acf_add_local_field_group(array(
'key' => 'group_5d146d18eeb92',
'title' => 'My Group Title',
'fields' => array(
array(
'key' => 'field_5d146d1f27577',
'label' => 'My Field Title',
'name' => 'my_field_name',
'type' => 'true_false',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array(
'width' => '',
'class' => '',
'id' => '',
),
'message' => '',
'default_value' => 0,
'ui' => 1,
'ui_on_text' => '',
'ui_off_text' => '',
),
),
'location' => array(
array(
array(
'param' => 'post_type',
'operator' => '==',
'value' => 'my_custom_post_type',
),
),
),
'menu_order' => 0,
'position' => 'side',
'style' => 'default',
'label_placement' => 'top',
'instruction_placement' => 'label',
'hide_on_screen' => '',
'active' => true,
'description' => '',
));
Check out the ACF page for registering fields via PHP.
Action acf/init is available for pro version only, maybe that was the reason it did not work at the first place..
For basic version you have to use acf/register_fields to register your custom fields.

Call PHP Wordpress shortcode - Warning: in_array() expects parameter 2 to be array, string given in

I am using wordpress and 2 plugins called unyson and brizy.
Basically unyson provides me a shortcode I want to call trough the front page builder brizy on my homepage.
I want to call the portfolio shortcode with [portfolio]
This works.
Live website: https://xprs.ch/
Under "Our Work Portfolio"
Now I want to add options:
[portfolio page_link="On" categories="website"]
As long as I just define page_link it works.
But as soon as I try to define the categories I get the following error:
Warning: in_array() expects parameter 2 to be array, string given in /home/httpd/vhosts/xprs.ch/httpdocs/wp-content/themes/jevelin/framework-customizations/extensions/shortcodes/shortcodes/portfolio/views/view.php on line 100
I also get above error if just defining categories so I need to define this different but how?
The code from the shortcode:
NOTE: I cannot change this code.
I need to pass the parameter "website" correctly in the front end.
$options = array(
'id' => array( 'type' => 'unique' ),
'general' => array(
'title' => esc_html__( 'General', 'jevelin' ),
'type' => 'tab',
'options' => array(
'style' => array(
'type' => 'radio',
'label' => esc_html__('Style', 'jevelin'),
'desc' => esc_html__('Choose main style', 'jevelin'),
'choices' => array(
'default' => esc_html__('Standard', 'jevelin'),
'default-shadow' => esc_html__('Standard with Shadow', 'jevelin'),
'default2' => esc_html__('Trendy', 'jevelin'),
'masonry' => esc_html__('Gallery', 'jevelin'),
'masonry2' => esc_html__('Marginless Gallery', 'jevelin'),
'minimalistic' => esc_html__('Minimalistic', 'jevelin'),
),
'value' => 'default',
),
'categories' => array(
'type' => 'multi-select',
'label' => esc_html__('Categories', 'jevelin'),
'desc' => esc_html__('Select categories', 'jevelin'),
'population' => 'taxonomy',
'source' => 'fw-portfolio-category',
'prepopulate' => 200,
'limit' => 100,
),
'page_link' => array(
'type' => 'switch',
'label' => esc_html__( 'Page Link', 'jevelin' ),
'desc' => esc_html__( 'Enable or disable portfolio page link', 'jevelin' ),
'value' => true,
'left-choice' => array(
'value' => false,
'label' => esc_html__('Off', 'jevelin'),
),
'right-choice' => array(
'value' => true,
'label' => esc_html__('On', 'jevelin'),
),
),
),
),
);
How do I need to call categories?
Here:
$cats = array();
foreach( get_the_category() as $cat ) {
$cats[] = $cat->cat_ID;
}
$args = array(
'post_status' => 'publish',
'posts_per_page' => -1,
'tax_query' => array(
array(
'taxonomy' => 'category',
'field' => 'id',
'terms' => $cats,
'include_children' => false
)
)
);
print_r( WP_Query( $args ) as $post );
http://codex.wordpress.org/Class_Reference/WP_Query

How to create dynamic fields in visual composer?

I am creating visual composer plugin for price table. I want to add dynamic textfield so user become able to add multiple text fields for item list as want.
For now it's showing only one text field but user should able to add multiple fields.
array (
"type" => "textfield",
"heading" => __( 'List Items', 'pt-vc' ),
"param_name" => "price_list",
"description" => __( 'Write the list item that you offer', 'pt-vc' ),
"group" => 'List Item',
),
You can use param_group. Here is the code example.
'params'=> array (
array(
'type' => 'param_group',
'value' => '',
'heading' => __( 'List Items', 'pt-vc' ),
'param_name' => 'price_list',
// Note params is mapped inside param-group:
'params' => array(
array(
'type' => 'textfield',
'value' => '',
'heading' => __( 'List Items', 'pt-vc' ),
'param_name' => 'list_itmes',
)
)
)
);
I think answer may be late but help others.
You may use param_group for that. It's not mentioned in the documentation but you may find it "How To's"
https://kb.wpbakery.com/docs/developers-how-tos/use-param-group-in-elements/
Code snippet from link (in case link expires again):
vc_map(
array(
'base' => 'your_shortcode',
'params' => array(
array(
'type' => 'textfield',
'value' => '',
'heading' => 'Title',
'param_name' => 'simple_textfield',
),
// params group
array(
'type' => 'param_group',
'value' => '',
'param_name' => 'titles',
// Note params is mapped inside param-group:
'params' => array(
array(
'type' => 'textfield',
'value' => '',
'heading' => 'Enter your title(multiple field)',
'param_name' => 'title',
)
)
)
)
)
)

Categories