Hi I am trying to change the way Fusion Builder container works. I want to add more custom fields, change the display html and the default variables.
I need to override the /plugin/fusion-builder/shorcode/fusion-container.php
I am using a child theme of avada. What is the correct way to override it?
Just right after I posted the question, I got the answer in GitHub:
https://github.com/Theme-Fusion/Fusion-Builder-Sample-Add-On/issues/34
The solution is to add to the functions.php in your child template the following code:
/**
* Filter already set maps, add in a new option to container.
*/
function filter_available_element( $fusion_builder_elements ) {
if ( isset( $fusion_builder_elements['fusion_builder_container'] ) ) {
$fusion_builder_elements['fusion_builder_container']['params'][] = array(
'type' => 'radio_button_set',
'heading' => esc_attr__( 'Container Design Mode', 'fusion-builder' ),
'description' => esc_attr__( 'Controls whether the container should be dark or light.', 'fusion-builder' ),
'param_name' => 'container_mode',
'value' => array(
'light' => esc_attr__( 'Light', 'fusion-builder' ),
'dark' => esc_attr__( 'Dark', 'fusion-builder' ),
),
'default' => 'light',
'group' => esc_attr__( 'Design', 'fusion-builder' ),
);
}
return $fusion_builder_elements;
}
add_filter( 'fusion_builder_all_elements', 'filter_available_element' );
/**
* Filter the parameters, check for container_mode and if set add to class parameter.
*/
function filter_container_parameters( $out, $pairs, $atts, $shortcode ) {
// If set, use it, otherwise set to default.
$out['container_mode'] = isset( $atts['container_mode'] ) ? $atts['container_mode'] : 'light';
// Set to class parameter which container already has and uses.
if ( ! isset( $out['class'] ) || '' === $out['class'] ) {
$out['class'] = 'my-class-' . $out['container_mode'];
} else {
$out['class'] .= ' my-class-' . $out['container_mode'];
}
return $out;
}
add_filter( 'shortcode_atts_fusion_builder_container', 'filter_container_parameters', 10, 4 );
Related
I am working with ACF Pro and ACF Theme Code Pro plugins.
It correctly working within admin-panel but not working on site.
Maybe anyone know what i doing incorrectly?
my functions.php
add_action( 'acf/init', 'register_about_service_block' );
function register_about_service_block() {
if ( function_exists( 'acf_register_block_type' ) ) {
// Register About service block
acf_register_block_type( array(
'name' => 'about-service',
'title' => __( 'About service' ),
'description' => __( 'A custom About service block.' ),
'category' => 'formatting',
'icon' => 'layout',
'keywords' => array( 'about', 'service' ),
'post_types' => array( 'post', 'page' ),
'mode' => 'auto',
// 'align' => 'wide',
'render_template' => 'template-parts/blocks/about-service.php',
// 'render_callback' => 'about_service_block_render_callback',
'enqueue_style' => get_template_directory_uri() . '/dist/css/services.min.css',
// 'enqueue_script' => get_template_directory_uri() . '/template-parts/blocks/about-service/about-service.js',
// 'enqueue_assets' => 'about_service_block_enqueue_assets',
));
}
}
Plugin genereted code that i can use in my template. I use it in template and it correctly working in admin-panel. It looks like this
But on my page, where i use this template it not working. Еhe template itself is connected correctly.
function acf_register_block_type must return $block
function acf_register_block_type( $block ) {
// Validate block type settings.
$block = acf_validate_block_type( $block );
// Bail early if already exists.
if( acf_has_block_type($block['name']) ) {
return false;
}
// Add to storage.
acf_get_store( 'block-types' )->set( $block['name'], $block );
// Register block type in WP.
if( function_exists('register_block_type') ) {
register_block_type($block['name'], array(
'attributes' => acf_get_block_type_default_attributes(),
'render_callback' => 'acf_rendered_block',
));
}
// Register action.
add_action( 'enqueue_block_editor_assets', 'acf_enqueue_block_assets' );
// Return block.
return $block;
}
Then $block must be use in my template but when i make var_dump($block) i get null. Hence i can't use data by $block in template
I am looking to make some modifications to a function in WooCommerce, on a file called class-wc-frontend-scripts.php in woocommerce/includes/
The function I'm looking to modify is:
private static function get_script_data( $handle ) {
global $wp;
switch ( $handle ) {
case 'wc-single-product' :
return array(
'i18n_required_rating_text' => esc_attr__( 'Please select a rating', 'woocommerce' ),
'review_rating_required' => get_option( 'woocommerce_review_rating_required' ),
'flexslider' => apply_filters( 'woocommerce_single_product_carousel_options', array(
'rtl' => is_rtl(),
'animation' => 'slide',
'smoothHeight' => true,
'directionNav' => false,
'controlNav' => 'thumbnails',
'slideshow' => false,
'animationSpeed' => 500,
'animationLoop' => false, // Breaks photoswipe pagination if true.
) ),
'zoom_enabled' => apply_filters( 'woocommerce_single_product_zoom_enabled', get_theme_support( 'wc-product-gallery-zoom' ) ),
'photoswipe_enabled' => apply_filters( 'woocommerce_single_product_photoswipe_enabled', get_theme_support( 'wc-product-gallery-lightbox' ) ),
'photoswipe_options' => apply_filters( 'woocommerce_single_product_photoswipe_options', array(
'shareEl' => false,
'closeOnScroll' => false,
'history' => false,
'hideAnimationDuration' => 0,
'showAnimationDuration' => 0
) ),
'flexslider_enabled' => apply_filters( 'woocommerce_single_product_flexslider_enabled', get_theme_support( 'wc-product-gallery-slider' ) ),
);
break;
}
return false;
}
For my product slider, I need to show prev and next arrows on single product page. So I need to change 'directionNav' as true.
How do I do this without making changes to the core files?
Have you created a child theme yet? If not, that's your first step. Here's a link from the WordPress codex:
https://codex.wordpress.org/Child_Themes
Then instead of editing that core file, you hook into that function instead, and write that code inside of the functions.php file you created in your child theme. If you want to add code to the existing function, you'll want to use an action hook. If you want to modify the code, you use a filter. It looks like you're trying to change the code, so probably a filter is best.
Here's how it would look using a filter:
add_filter( 'get_script_data', 'change_nav_direction' );
function change_nav_direction( $variable ) {
//any other code you may need
'directionNav' => true,
//you must have a return
return $variable;
}
Here's a link to an article from WooCommerce that might help:
https://docs.woocommerce.com/document/introduction-to-hooks-actions-and-filters/
I have this situation - I made a changes in one of the woocommerce email templates, but I`m sure - these changes will be lost after next woocommerce update.
As I know, I should use theme functions to bypass this problem.
This is the code before changes:
echo '<ul class="wc-bacs-bank-details order_details bacs_details">' . PHP_EOL;
// BACS account fields shown on the thanks page and in emails
$account_fields = apply_filters( 'woocommerce_bacs_account_fields', array(
'account_number'=> array(
'label' => __( 'Account Number', 'woocommerce' ),
'value' => $bacs_account->account_number
),
'sort_code' => array(
'label' => $sortcode,
'value' => $bacs_account->sort_code
),
'iban' => array(
'label' => __( 'IBAN', 'woocommerce' ),
'value' => $bacs_account->iban
),
'bic' => array(
'label' => __( 'BIC', 'woocommerce' ),
'value' => $bacs_account->bic
)
), $order_id );
foreach ( $account_fields as $field_key => $field ) {
if ( ! empty( $field['value'] ) ) {
echo '<li class="' . esc_attr( $field_key ) . '">' . esc_attr( $field['label'] ) . ': <strong>' . wptexturize( $field['value'] ) . '</strong></li>' . PHP_EOL;
}
}
echo '</ul>';
Here is the custom account field code that I want to insert:
'merkis' => array(
'label' => $merkis,
'value' => $pasutijums
)
How can I insert my custom code without overriding that core file?
Thanks
Never Override core files and always use the WooCommerce included hooks to make code customizations.
If you haven't find the way to make this change through a custom hooked function, as you will see in your provided code, you can use woocommerce_bacs_account_fields filter hook to add your custom code, without overriding any WooCommerce core files.
So the code for adding a new field in BACS account fields, is going to be:
add_filter( 'woocommerce_bacs_account_fields', 'custom_bacs_account_field', 10, 2);
function custom_bacs_account_field( $account_fields, $order_id ) {
$account_fields['merkis' ] = array(
'label' => $merkis,
'value' => $pasutijums
);
return $account_fields;
}
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
This code is tested and works…
Having a bit of bother with the Wordpress Meta Box plugin, specifically with retrieving an image url from an image added to a custom post type.
I'm creating meta boxes in a custom plugin, like so:
add_filter( 'rwmb_meta_boxes', 'xxx_meta_register_meta_boxes' );
function xxx_meta_register_meta_boxes( $meta_boxes )
{
$prefix = 'xxx_meta_';
$meta_boxes[] = array(
'title' => esc_html__( 'Retailer Information', '' ),
'id' => 'advanced',
'post_types' => array( 'xxx_retailers' ),
'autosave' => true,
'fields' => array(
// PLUPLOAD IMAGE UPLOAD (WP 3.3+)
array(
'name' => esc_html__( 'Retailer Logo', '' ),
'id' => "{$prefix}plupload",
'type' => 'plupload_image',
'max_file_uploads' => 1,
),
// URL
array(
'name' => esc_html__( 'Link', '' ),
'id' => "{$prefix}url",
'desc' => esc_html__( 'Clicking the retailer logo will take the user to this URL', '' ),
'type' => 'url',
'std' => 'xxx',
),
)
);
return $meta_boxes;
}
So far so good, these boxes are relevant to a custom post type 'xxx_retailers'.
The problem comes with retrieving this data. I want to display my retailers in a widget. I've chopped and changed another piece of code I've used previously, but it's not returning the image URL, just the ID. Unfortunately I don't know enough php to figure out why.
// Create Retailers Widget
// Create the widget
class Retailers_Widget extends WP_Widget {
function __construct() {
parent::__construct(
// base ID of the widget
'retailers_widget',
// name of the widget
__('XXX Retailers List', '' ),
// widget options
array (
'description' => __( 'Shows a list of retailer logos', '' )
)
);
}
function widget( $args, $instance ) {
// kick things off
extract( $args );
echo $before_widget;
echo $before_title . 'Retailers' . $after_title;
// Pull through Retailers
$xxxretailers = get_posts(array(
'post_type' => 'xxx_retailers',
'orderby' => 'title',
'order' => 'asc',
));
// Display for each Retailer
foreach ($xxxretailers as $xxxretailer) {
$custom = get_post_custom($xxxretailer->ID);
$meta_ret_img = $custom["xxx_meta_plupload"][0];
$meta_ret_url = $custom["xxx_meta_url"][0];
// Display Retailers
echo "<li><a href='{$meta_ret_url}'><img src='{$meta_ret_img}' /></a></li>";
}
}
};
// Register widget
function register_retailers_widget() {
register_widget( 'Retailers_Widget' );
}
add_action( 'widgets_init', 'register_retailers_widget' );
The URLs are coming through correctly, so I know this is a problem with the line
$meta_ret_img = $custom["xxx_meta_plupload"][0];
But I can't figure out how to get the image URL from the data I presume is stored as an array. Any ideas?
Edit:
I should have mentioned, in a single post I can get a single image with:
$images = rwmb_meta( 'xxx_meta_plupload', 'size=medium' );
if ( !empty( $images ) ) {
foreach ( $images as $image ) {
echo "<img src='{$image['url']}' />";
}
}
But I want to show images from all my retailers post types, to create a list of logos.
Replace this statement:
$meta_ret_img = $custom["xxx_meta_plupload"][0];
with this:
$meta_ret_img_array = wp_get_attachment_image_src($custom["xxx_meta_plupload"][0]);
$meta_ret_img = $meta_ret_img_array[0];
also please remove all these curly braces from src and href attributes from your code.
if you are interested in any particular size of the image, then see the official document of wp_get_attachment_image_src() function here.
For e.g. for medium size image you can write it as:
wp_get_attachment_image_src($custom["xxx_meta_plupload"][0], 'medium');
I have been attempting to create a theme reset button for the Wordpress theme customizer that automatically removes all of the theme_mod settings. I have tried multiple ways to do this and have never been able to get it to work. As seen here.
After multiple failed attempts using the remove_theme_mods approach I begin to wonder if that is my problem other than the ajax and javascript being faulty and not properly binding the button.
Since I save all defaults into one big array so when my theme is installed it automatically has values populated on the theme customizer page, and the theme has a specific look... I was thinking I could try a different approach to instead remove the theme settings I just over ride them, maybe with a custom control? Possibly by somehow assigning these default values to all settings? I really hope someone can help me get this going. If you have any ideas I would appreciate it very very much.
Here is an example of how I assign the default values for the theme:
function newtheme_get_theme_mods() {
$defaults = array(
'newtheme_responsive' => true,
'newtheme_hero_title' => 'Beautiful and Elegant',
'newtheme_hero_description' => 'The best theme in the world',
'newtheme_header_logo' => '/wp-content/themes/newtheme/img/logo.png',
'newtheme_header_background_color' => 'rgba(35,35,35, 0.9)'
return $defaults;
}
Create a Class Theme and store theme settings in database with the serialized object of class theme. On changing the theme or restoring the theme load that serialized object in your Theme class constructor and then the class will use the settings accordingly. That is some thing I have done with my project so far.
As answered on Stack Exchange
The problem with using remove_theme_mods for showing defaults in the Customizer is
the Customizer is a preview which you can exit without saving,
the individual theme_mods are filtered, but not the entire theme_mod array
theme_mods includes menus and widgets.
I also wanted a reset button, but I chose instead to create a Preset control, and one of the presets is "defaults". This way uses a select, so there is no problem with the button not working (because bind is for value changes and buttons don't change their values).
The trick is to use ajax to retrieve the chosen preset, and then loop over the values in javascript, assigning them to the settings so that those changes will trigger the refresh of the preview. My code includes filters so that child themes can add in more options and presets. And the presets can be subsets of the options available.
Here is PHP for the Preset control (just a normal select, but a settingless control):
$wp_customize->add_control( 'option_presets', array(
'label' => __( 'Use preset theme options', 'mytheme' ),
'description' => __( 'Theme options will be set to the preset values.', 'mytheme' ),
'section' => 'mytheme_section',
'settings' => array(),
'type' => 'select',
'capability' => 'edit_theme_options',
'choices' => mytheme_option_presets_choices(),
) );
Here is the rest of the PHP functions.
/**
* Supply list of choices for option presets.
*/
function mytheme_option_presets_choices() {
return apply_filters( 'mytheme_option_presets_choices', array(
'none' => __( 'Select preset', 'mytheme' ),
'defaults' => __( 'Defaults', 'mytheme' ),
'dark' => __( 'Dark', 'mytheme' ),
) );
}
/**
* Sanitize an option preset choice.
*/
function mytheme_sanitize_option_presets_choice( $input ) {
$valid = mytheme_option_presets_choices();
return array_key_exists( $input, $valid ) ? $input : 'none';
}
/**
* Get the preset values for the chosen option preset.
*/
function mytheme_option_preset( $which ) {
$values = array();
if ( 'defaults' === $which ) {
$values = mytheme_default_values();
}
if ( 'dark' === $which ) {
$values = array(
'body_textcolor' => '#f9f7f7',
'background_color' => '#444244',
'header_textcolor' => '#bf9a07',
'area_classes' => array(
'sidebar' => 'semi-black',
'widgets' => 'box',
),
);
}
return apply_filters( 'mytheme_option_preset', $values, $which );
}
/**
* Add a nonce for Customizer for option presets.
*/
function mytheme_refresh_nonces( $nonces ) {
$nonces['mytheme-customize-presets'] = wp_create_nonce( 'mytheme-customize-presets' );
return $nonces;
}
add_filter( 'customize_refresh_nonces', 'mytheme_refresh_nonces' );
/**
* Ajax handler for supplying option preset values.
*/
function mytheme_ajax_option_preset_values() {
check_ajax_referer( 'mytheme-customize-presets', 'option_presets_nonce' );
if ( ! current_user_can( 'edit_theme_options' ) ) {
wp_die( -1 );
}
if ( empty( $_POST['option_preset'] ) ) {
wp_send_json_error( 'mytheme_missing_preset_parameter' );
}
$preset = sanitize_text_field( wp_unslash( $_POST['option_preset'] ) );
$values = mytheme_option_preset( $preset );
if ( empty( $values ) ) {
wp_send_json_error( array( 'message' => __( 'No preset found.', 'mytheme' ) ) );
}
else { // Flatten the array.
foreach ($values as $key => $avalue) {
if ( is_array( $avalue ) ) {
unset( $values[$key] );
foreach ($avalue as $subkey => $subvalue) {
$values[$key . '[' . $subkey . ']'] = $subvalue;
}
}
}
wp_send_json_success( array( 'values' => $values ) );
}
}
add_action( 'wp_ajax_mytheme_option_preset', 'mytheme_ajax_option_preset_values' );
And then just a little bit of Javascript to make the ajax request. This is queued on the 'customize_controls_enqueue_scripts' action. (I left out the display of the error message.)
wp.customize.control( 'option_presets', function( control ) {
control.element = new wp.customize.Element( control.container.find( 'select' ) );
control.element.bind( function( preset ) {
var request = wp.ajax.post( 'mytheme_option_preset', {
option_presets_nonce: wp.customize.settings.nonce['mytheme-customize-presets'],
wp_customize: 'on',
customize_theme: wp.customize.settings.theme.stylesheet,
option_preset: preset
} );
request.done( function( response ) {
_.each( response.values, function( value, id ) {
var setting = wp.customize( id );
if ( setting ) {
setting.set( value );
}
} );
} );
} );
} );