I'm creating custom ACF Gutenberg blocks my a site and have successfully managed to register my own blocks. Now, I have a custom post type called Blog. I do not want blog to show all my ACF Gutenberg blocks, I want to create a separate batch for custom post type use only. I have enabled show_in_rest, but even the default Gutenberg blogs do not show for me?
Here is my approach:
1. Registering the post type (theme/functions.php)
<?php
register_post_type('Blog', theme_build_post_args('Blog', 'Blog', 'Blog', array(
'show_in_rest' => true,
'menu_icon' => 'dashicons-edit',
'menu_position' => 20,
'has_archive' => true,
'public' => true,
'supports' => array(
'editor',
'title',
'author',
'revisions',
'excerpt',
'thumbnail'
) ,
)));
?>
2. Registering the ACF Gutenberg blocks for pages (theme/inc/acf-blocks/blocks.php)
Here are the blocks that I've registered for use on pages (not on the blog post type):
<?php
$hero = array(
'name' => 'hero',
'title' => __('Hero') ,
'description' => __('') ,
'render_callback' => 'block_render',
'category' => 'formatting',
'icon' => 'admin-comments',
'keywords' => array(
'hero'
) ,
);
$blocks = [$hero];
return $blocks;
?>
Registering the ACF Gutenberg blocks for blog post type (theme/inc/acf-blocks/blog-blocks.php)
<?php
$blog_hero = array(
'name' => 'blog_hero',
'title' => __('Blog hero') ,
'description' => __('') ,
'render_callback' => 'block_render',
'category' => 'formatting',
'icon' => 'admin-comments',
'keywords' => array(
'hero',
'blog'
) ,
);
$blog_blocks = [$blog_hero];
return $blog_blocks;
?>
Register all blocks (theme/inc/acf-blocks/functions.php)
<?php
/*
* loop though array and register each block type
*/
function block_acf_init(){
$path = get_template_directory().'/inc/acf-blocks/blocks.php';
$blocks = require($path);
foreach($blocks as $block) {
acf_register_block_type($block);
}
}
function blog_acf_init(){
$path = get_template_directory().'/inc/acf-blocks/blog-blocks.php';
$blog_blocks = require($path);
foreach($blog_blocks as $blog_block) {
acf_register_block_type($blog_block);
}
}
// Check if function exists, and hook into setup
if( function_exists('acf_register_block_type') ) {
add_action('acf/init', 'block_acf_init');
add_action('acf/init', 'blog_acf_init');
}
?>
Current results:
When creating a post on the blog custom post type, I do not have the ability to add any blocks, let alone see if blog_hero block appears:
On pages, I can see all my created blocks, however, the blog hero block shows on the page side, when I only want it for the custom post type:
Probably this way could solve the problem:
Specifying post_types param for Blog Hero block to blog
$blog_hero = array(
'name' => 'blog_hero',
'title' => __( 'Blog hero', 'Context' ),
'description' => __( '', 'Context' ),
'render_callback' => 'block_render',
'category' => 'formatting',
'icon' => 'admin-comments',
'keywords' => array(
'hero',
'blog'
),
'post_types' => array( 'blog' ),
);
And analogically specifying all post types except blog for Hero block.
$all_post_types = get_post_types();
$hero_block_post_types = array_diff( $all_post_types, array( 'blog' ) );
$hero = array(
'name' => 'hero',
'title' => __( 'Hero', 'Domain' ),
'description' => __( '', 'Domain' ),
'render_callback' => 'block_render',
'category' => 'formatting',
'icon' => 'admin-comments',
'keywords' => array(
'hero'
),
'post_types' => $hero_block_post_types
);
$blocks = [ $hero ];
return $blocks;
Notice:
Consider adding Domain for your __ function.
Good practice to use __ function with Domain.
Related
I'm using Metabox Plugin in WP and generate a field using MB online generator.
add_filter( 'rwmb_meta_boxes', 'bq_homemeta_register_meta_boxes' );
function bq_homemeta_register_meta_boxes( $meta_boxes ) {
$prefix = '';
$meta_boxes[] = [
'title' => esc_html__( 'Homepage Meta', 'bequik' ),
'id' => 'home_meta',
'context' => 'normal',
'pages' => 'page',
'fields' => [
[
'type' => 'image_advanced',
'name' => esc_html__( 'Hero Image', 'bequik' ),
'id' => $prefix . 'hero_image',
'max_file_uploads' => 1,
],
],
];
return $meta_boxes;
}
I wanted the metabox field to display only in home page in the WP admin editor.
I tried the include/exclude extension but failed to work.
Thank you!
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.
I've tried many things to fix this, I'm fairly new to coding a plugin but I can't seem to get this to work.
I have created a custom post type ("Book") perfectly fine. Now I'm trying to add meta-boxes into it. I can't seem to get the coding to connect to each other properly.
I've tried adding the slugs of each meta box to the "support", I've tried various instances of function and add_meta_box as well as putting different things under "register_meta_box_cb" and nothing I'm doing seems to be working.
Any help is appreciated.
// Register Custom Post Type
function post_type_book() {
$args = array(
'label' => __( 'book', 'author_station' ),
'description' => __( 'Custom Book Entry', 'author_station' ),
'supports' => array('title'),
'taxonomies' => array( 'genres', 'series', 'tags' ),
'register_meta_box_cb' => ('as_add_book' ),
'labels' => $labels,
'hierarchical' => false,
'public' => true,
'show_ui' => true,
'show_in_menu' => false,
'menu_position' => 20,
'show_in_admin_bar' => true,
'show_in_nav_menus' => true,
'can_export' => true,
'has_archive' => true,
'exclude_from_search' => false,
'publicly_queryable' => true,
'rewrite' => array('slug' => 'Book'),
'capability_type' => 'page',
);
register_post_type( 'as_book', $args );
}
function as_add_book( $meta_boxes ) {
add_meta_box(
'add_meta_boxes',
array( $this, 'as_add_book_boxes' ));
$types = array('post', 'page', 'book');
if (in_array($types)) {
add_meta_box(
'as_add_book',
'add_meta_boxes',
'Book',
'as_add_book_callback',
$types,
'normal',
'high'
);
}
$prefix = 'as_';
$meta_boxes[] = array(
'id' => 'as_add_book',
'title' => esc_html__( 'Book', 'author_station_book' ),
'pages'=> array('as_book'),
'context' => 'advanced',
'priority' => 'high',
'autosave' => 'false',
'fields' => array(
array(
'id' => $prefix . 'book_cover',
'type' => 'image',
'name' => esc_html__( 'Book Cover', 'author_station_book' ),
),
array(
'id' => $prefix . 'book_title',
'type' => 'text',
'name' => esc_html__( 'Title', 'author_station_book' ),
),
);
return $meta_boxes;
}
add_filter( 'rwmb_meta_boxes', 'as_add_book' );
add_action( 'add_meta_boxes', 'my_custom_meta_box' ) );
function my_custom_meta_box(){
$args = array();
add_meta_box(
'my_metabox_id',
__( 'My Meta Box', 'my_textdomain' ), // Title
'my_callback_function', // Callback function that renders the content of the meta box
'post', // Admin page (or post type) to show the meta box on
'side', // Context where the box is shown on the page
'high', // Priority within that context
$args // Arguments to pass the callback function, if any
);
}
function my_callback_function( $args ){
//The markup for your meta box goes here
}
I am building a website in where I want to show the tag selected from the custom fields (mainly from radio button). I have setup the cmb2 as like below the codes..
add_action('cmb2_admin_init', 'custom_metaboxes');
function custom_metaboxes() {
$metabox = new_cmb2_box( array(
'object_types' => array( 'post'), //for the post
'title' => 'Additional Fields',
'id' => 'additional'
)
);
// showing in the admin panel
$metabox -> add_field( array(
'name' => 'Taxonomy List',
'desc' => 'This get the list of taxonomy',
'id' => 'taxonomy_list',
'type' => 'taxonomy_radio',
'taxonomy' => 'post_tag',
'default' => 'ami'
)
);
}
Ok, that is working in the post section. My tags are shown in the radio buttons, That's working. But when I tried to show the the selected tag in the front-end using
echo get_post_meta( get_the_id(), 'taxonomy_list', true )// returns nothing
nothing is echoing. Then tried var_dump function it returns string(0) "". What are the problems are working behind the scene.
Anyone please find out what the problems are.
The problem can be solved by using cmb2-2.0.2 version. And here is the code:
<?php
add_action( 'cmb2_init', 'yourprefix_register_demo_metabox' );
function yourprefix_register_demo_metabox() {
$prefix = '_yourprefix_demo_';
$cmb_demo = new_cmb2_box( array(
'id' => $prefix . 'additional',
'title' => 'Additional Fields',
'object_types' => array('page')
) );
$cmb_demo->add_field( array(
'name' => 'Taxonomy List',
'desc' => 'This get the list of taxonomy',
'id' => $prefix . 'taxonomy_list',
'type' => 'taxonomy_radio',
'taxonomy' => 'post_tag'
) );
}
?>
And in frontend you need to write:
<?php
$prefix = '_yourprefix_demo_';
echo get_tag(get_post_meta(get_the_ID(), $prefix.'taxonomy_list', true)[0])->name;
?>
I am trying to add an excerpt option to my category page, to display instead of my description.
So basically, I need a box on this screen which will be able to be used as preview text.
The code used to create this taxonomy is:
add_action( 'init', 'create_product_cat_external' );
function create_product_cat_external() {
register_taxonomy(
'ExternalProducts',
'products',
array(
'label' => __( 'External Products' ),
'rewrite' => array( 'slug' => 'externalproducts' ),
'hierarchical' => true,
)
);
}
and the box needs to be here:
You can use CMB2 plugin and put this in your functions.php for example:
add_action( 'cmb2_admin_init', 'yourprefix_register_taxonomy_metabox' );
function yourprefix_register_taxonomy_metabox() {
$prefix = 'yourprefix_term_';
$cmb_term = new_cmb2_box( array(
'id' => $prefix . 'edit',
'object_types' => array( 'term' ), // Tells CMB2 to use term_meta vs post_meta
'taxonomies' => array( 'products'), // Tells CMB2 which taxonomies should have these fields
// 'new_term_section' => true, // Will display in the "Add New Category" section
) );
$cmb_term->add_field( array(
'name' => __('Excerpt', 'default'),
'id' => $prefix . 'excerpt',
'type' => 'wysiwyg',
'on_front' => false,
) );
}