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
Related
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 );
So I ran into a issue where the customizer preview doesn't fully refresh. Only when I manually refresh the page I see my changes. Some of my code to help explain below.
For my customizer settings I have code that looks something like this
$wp_customize->add_section( 'theme_layout', array(
'title' => __( 'Layout', 'theme' ),
'priority' => 30
) );
$wp_customize->add_setting( 'theme_header_layout', array(
'default' => 'default',
'transport' => 'refresh',
) );
$wp_customize->add_control( new WP_Customize_Control( $wp_customize,
'theme_header_layout', array(
'label' => __( 'Header', 'theme' ),
'section' => 'theme_layout',
'settings' => 'theme_header_layout',
'type' => 'select',
'choices' => array(
'default' => 'default',
'special_header' => 'Special Header',
)
) ) );
Now In my functions.php I have code like this
//this is the code that doesn't seem to execute when the customizer refreshes
if ( 'special_header' == get_theme_mod( 'theme_header_display' ) ):
function theme_special_header( $items ) {
$items .= do_shortcode('[special_header_shortcode]');//This shortcode exists I just didnt bother mentioning it here
}
add_action( 'wp_nav_menu_secondary-menu_items', 'theme_special_header' );//Adds shortcode to menu with id of secondary-menu
endif;
This all works great accept when I go to the customizer and select 'Special Header' the customizer refreshes and I don't see my changes until I completely refresh the page.
I had also faced similar issue earlier. Rather than adding conditional outside, I kept it inside the function and it worked. You can try similar approach for your code and it may help.
Following is not exact answer for your question but it may help to fix your problem.
function wpso_customize_menu($items, $args) {
if ( 'special_header' == get_theme_mod( 'theme_header_layout' ) ) {
if( $args->theme_location == 'menu-1' ) {
$items .= '<li>Custom Link</li>';
}
}
return $items;
}
add_filter('wp_nav_menu_items', 'wpso_customize_menu', 10, 2);
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/
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'm being requested by my client to add a custom field that they will be able to enter in a url. The post itself is a custom plugin custom post type, this is the code I have for this portion:
register_post_type( 'storylist',
array(
'labels' => $labels,
'public' => false,
'exclude_from_search' => true,
'publicly_queryable' => false,
'show_ui' => true,
'supports' => array('title'),
)
);
add_filter( 'rwmb_meta_boxes', 'c_register_meta_boxes' );
}
function c_register_meta_boxes( $boxes ){
$prefix = 'c_rwmb_';
$boxes[] = array(
'id' => 'view',
'title' => __('View Link', 'c_rwmb' ),
'post_types' => array('storylist'),
'context' => 'normal',
'priority' => 'high',
'fields' => array(
array(
'name' => __('View URL', 'c_rwmb' ),
'id' => $prefix . 'view_url',
'type' => 'text',
'size' => 60,
'clone' => false
),
)
);
return $meta_boxes;
}
Now the problem is when I go to the post, I do not see the custom meta field even showing up, is there something that I'm missing?
The custom post type("storylist") comes from the plugin right? Then you don't need to register the custom post again. You just needs to add meta field for this post type and save its value while updating the post. Once I had a experience to enable/disable sidebar using custom field. I shared my code. Hope this will help you.
<?php
add_action('admin_init','add_metabox_post_sidebar');
add_action('save_post','save_metabox_post_sidebar');
/*
* Funtion to add a meta box to enable/disable the posts.
*/
function add_metabox_post_sidebar()
{
add_meta_box("Enable Sidebar", "Enable Sidebar", "enable_sidebar_posts", "post", "side", "high");
}
function enable_sidebar_posts(){
global $post;
$check=get_post_custom($post->ID );
$checked_value = isset( $check['post_sidebar'] ) ? esc_attr( $check['post_sidebar'][0] ) : 'no';
?>
<label for="post_sidebar">Enable Sidebar:</label>
<input type="checkbox" name="post_sidebar" id="post_sidebar" <?php if($checked_value=="yes"){echo "checked=checked"; } ?> >
<p><em>( Check to enable sidebar. )</em></p>
<?php
}
/*
* Save the Enable/Disable sidebar meta box value
*/
function save_metabox_post_sidebar($post_id)
{
// Bail if we're doing an auto save
if( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;
// if our current user can't edit this post, bail
if( !current_user_can( 'edit_post' ) ) return;
$checked_value = isset( $_POST['post_sidebar'] ) ? 'yes' : 'no';
update_post_meta( $post_id, 'post_sidebar', $checked_value );
}
?>
Here I have added a custom field called 'post_sidebar' for post type "post" you can change your own and change your post type in this line add_meta_box("Enable Sidebar", "Enable Sidebar", "enable_sidebar_posts", "post", "side", "high"); from "post" to "storylist".
Just for somebody who needs to add MetaBox fields via plugin, because the question is:
How to add custom fields to a WordPress Plugin
function gffgfg_add_boxes( $meta_boxes ) {
$prefix = 'some prefix';
$meta_boxes[] = array(
//metabox array
);
return $meta_boxes;
}
add_filter( 'rwmb_meta_boxes', 'gffgfg_add_boxes', 999 ); // 999