Trying to create dynamic widget area using post ID - wordpress

In functions.php I added this code.
function page_widget_init( $post_id = 0 ) { {
register_sidebar( array(
'name' => __( 'Page Widgets', 'seven_theme' ),
'id' => 'page-widgets-' . $post_id,
'description' => __( 'To display Layers Widgets in front page, before your posts, after header.', 'seven_theme' ),
) );
}
add_action( 'widgets_init', 'page_widget_init', 100 );
First how to get post id in functions.php register_sidebar $post_id
Now going to page template which i created using page.php, called Blog Template
Call this widget in this page.
dynamic_sidebar( 'page-widgets-' . $post->ID );
My idea is to create unique widget content using post ID.

Related

How do I create a shortcode for a WordPress sidebar?

I have added a custom sidebar to my WordPress installation using the following script that also creates a shortcode:
// Register Sidebars
function custom_sidebars() {
$args = array(
'id' => 'gutenbar',
'class' => 'abeng',
'name' => __( 'Abeng sidebar', 'text_domain' ),
'description' => __( 'Sidebar for block', 'text_domain' ),
);
register_sidebar( $args );
}
add_action( 'widgets_init', 'custom_sidebars' );
add_shortcode( 'add_sidebar', 'custom_sidebars' );
When I add the [add_sidebar] shortcode to a Gutenberg block the sidebar does not appear. However, when I use a plugin called Sidebar Shortcode [sidebar id="gutenbar"] the sidebar is displayed perfectly. I assume that my shortcode is associated with a specific function and does not need a name or id for the sidebar to be chosen.
I had been thinking that because the sidebar was added using a plugin that allows the insertion of functions available site-wide rather than only in a theme using its functions.php, that was the reason the shortcode did not do its job. But since the sidebar shows in the widgets area and allowed me to add widgets, and now works with this plugin, that's obviously not the issue.
Looking under the hood of the concisely written Sidebar Shortcode, I can't see what I might want to add to make my code functional.
Why am I doing this? I'm using full-width pages for my posts, disabling the theme's default right sidebar (which doesn't play well in a responsive design) but still need to reinsert the sidebar in a column.
Thanks for any help.
With the help of Prashant Singh on the WordPress support forum, I learned that the function that creates the sidebar cannot be used to create a shortcode to place it on a page. The shortcode has to be created calling the WP native function dynamic_sidebar() that gives access to all sidebars (https://developer.wordpress.org/reference/functions/dynamic_sidebar/). Below is the full script that includes cleanup code to allow the correct positioning inside a page.
/**
* Create sidebar and shortcode
*/
// Register Sidebars
function custom_sidebars() {
$args = array(
'id' => 'gutenbar',
'class' => '',
'name' => __( 'Abeng sidebar', 'text_domain' ),
'description' => __( 'Sidebar for block', 'text_domain' ),
);
register_sidebar( $args );
}
add_action( 'widgets_init', 'custom_sidebars' );
/* Add the shortcode and allow positioning in page*/
add_shortcode( 'add_sidebar', 'my_custom_sidebar' );
function my_custom_sidebar(){
ob_start();
dynamic_sidebar( 'gutenbar');
$sidebar_left = ob_get_clean();
$html = ' <div class="sidebar-content"> ' . $sidebar_left . ' </div> ';
return $html;
}

Removing specific widgets from specific pages

I have specific pages that I need specific widgets to be hidden from the sidebar. I have
-5 pages that are supposed to only display nav_menu-1
-6 other pages that are supposed to display only nav_menu-2
-and then all single-posts that are supposed to only show nav_menu-3
I have this code
add_filter( 'widget_display_callback', 'widget_display_2', 50, 3 );
function widget_display_2( $instance, $widget, $args ){
if ( is_single() && $widget->id == 'nav_menu-1' ) {
return false;
}
return $instance;
}
but what is the best way to insert all my pages and widget IDs to achieve my goal?
It may sound weird but why not using three different sidebars and call them by condition. This way your Theme would become more flexible. It´s always bad to hardcode this into your theme or plugin.
// Register Sidebars
function custom_sidebars() {
$args = array(
'id' => 'custom-sidebar-1',
'name' => __( 'Custom Sidebar 1', 'text_domain' ),
);
register_sidebar( $args );
$args = array(
'id' => 'custom-sidebar-2',
'name' => __( 'Custom Sidebar 2', 'text_domain' ),
);
register_sidebar( $args );
$args = array(
'id' => 'custom-sidebar-3',
'name' => __( 'Custom Sidebar 3', 'text_domain' ),
);
register_sidebar( $args );
}
add_action( 'widgets_init', 'custom_sidebars' );
... after you added this to your functions.php you can call the different sidebars with conditional functions.
<?php
/** call custom sidebar on single.php template **/
if ( is_single() ){
dynamic_sidebar( 'custom-sidebar-1' );
}
?>
<?php
/** call custom sidebar on page.php template (all pages) **/
if ( is_page() ){
dynamic_sidebar( 'custom-sidebar-2' );
}
?>
<?php
/** call custom sidebar on multiple pages by id **/
if ( is_page( array( 11, 22, 33, 44 ) ) ){
dynamic_sidebar( 'custom-sidebar-3' );
}
?>

WPBakery Grid builder get post content

I am trying to show full content of Post in WpBakerys Grid builder
I can only add Post Excerpt but i would like to add full content not summary.I saw that I can add custom field but don't know Field key name of Post Content.
In short how can i show full post content instead of Excerpt?
You can add a custom shortcode to the Grid Builder:
add_filter( 'vc_grid_item_shortcodes', 'my_module_add_grid_shortcodes' );
function my_module_add_grid_shortcodes( $shortcodes ) {
$shortcodes['vc_post_content'] = array(
'name' => __( 'Post Content', 'my-text-domain' ),
'base' => 'vc_post_content',
'category' => __( 'Content', 'my-text-domain' ),
'description' => __( 'Show current post content', 'my-text-domain' ),
'post_type' => Vc_Grid_Item_Editor::postType(),
);
return $shortcodes;
}
// output function
add_shortcode( 'vc_post_content', 'vc_post_content_render' );
function vc_post_content_render() {
return '<p>{{ post_data:post_content }}</p>'; // usage of template variable post_data with argument "post_content"
}
Source: https://kb.wpbakery.com/docs/developers-how-tos/adding-custom-shortcode-to-grid-builder/#AddingCustomShortcodetoGridBuilder-Templatevariablesusage

how can i display product's custom field in wp-admin/edit.php?post_type=product ( product listing table in admin ) woocommerce

I have added some custom fields in woocommerce when adding product. Now i want to display those custom fields in product listing page ( wp-admin/edit.php?post_type=product ). I also want to quick edit and save like other values can be edit and save in listing page.
I tried bellow code but it did not work.
add_filter( 'manage_edit-product', 'show_product_order' );
function show_product_order($columns){
$new_columns = (is_array($columns)) ? $columns : array();
unset( $new_columns['order_actions'] );
$new_columns['product_order'] = 'Product Order';
$new_columns['order_actions'] = $columns['order_actions'];
return $new_columns;
}
I also tried below code but it did not worked too.
add_action( 'woocommerce_product_options_general_product_data', 'woocommerce_general_product_data_custom_field' );
function woocommerce_general_product_data_custom_field() {
global $woocommerce, $post;
echo '<div class="options_group">';
woocommerce_wp_checkbox(
array(
'id' => 'product_order',
'wrapper_class' => 'checkbox_class',
'label' => __('Order for Product', 'woocommerce' ),
'description' => __( 'Order for Product', 'woocommerce' )
)
);
echo '</div>';
}
i also reffered this post WooCommerce show custom column but i did not succeed to get solution
Please help.

Update wordpress post from front-end (using acf)

I am making front end admin for users to add/edit their posts. I already made post add form and it works, but edit form doesnt work.
functions.php
function add_new_post( $post_id )
{
if( $post_id == 'new' ) {
// Create a new post
$post = array(
'post_title' => $_POST["fields"]['field_52c810cb44c7a'],
'post_category' => array(4),
'post_status' => 'draft',
'post_type' => 'post'
);
// insert the post
$post_id = wp_insert_post( $post );
return $post_id;
}
else {
return $post_id;
}
}add_filter('acf/pre_save_post' , 'add_new_post' );
index.php
<div id="updateform-<?php the_ID(); ?>" class="collapse">
<?php
echo get_the_ID();
$args = array(
'post_id' => get_the_ID(), // post id to get field groups from and save data to
'field_groups' => array(31), // this will find the field groups for this post (post ID's of the acf post objects)
'form' => true, // set this to false to prevent the <form> tag from being created
'form_attributes' => array( // attributes will be added to the form element
'id' => 'post',
'class' => '',
'action' => get_permalink( get_the_ID() ),
'method' => 'post',
),
'return' => add_query_arg( 'updated', 'true', get_permalink() ), // return url
'html_before_fields' => '', // html inside form before fields
'html_after_fields' => '', // html inside form after fields
'submit_value' => 'Update', // value for submit field
'updated_message' => 'Post updated.', // default updated message. Can be false to show no message
);
acf_form( $args );
?>
</div>
For saving posts you should use save_post not pre_save_post.
Basically pre_save_post is used for new posts.
Use this below add_filter('save_post' , 'add_new_post');
I created plugins for that:
Forms actions
https://wordpress.org/plugins/forms-actions/
Add actions to yours ACF forms.
ACF Frontend display
https://wordpress.org/plugins/acf-frontend-display/
Display your ACF form on frontend
If by some chance you happen to be using Elementor Page Builder, you can do this by installing Advanced Widgets for Elementor. It comes with an ACF Form widget that you can just drag & drop anywhere into your site and configure your form through the UI (no coding required).

Resources