Just wondering if someone could give me some advice on how to create unlimited sidebars for a WordPress theme.
So, basically I am looking into adding a meta box on posts and pages with a drop down list of all available sidebars, where the user can then select a specific sidebar for any post or page. This part I have figured out, however I would also like to add functionality here to allow users to create custom sidebars for any post or page. So, essentially the user could choose any sidebar for any post or page or create new sidebars for any post or page which then could be filled with widgets on the widgets page.
I half there, just not sure how to go about creating the functionality to create new sidebars.
There is a way to do it but it's a bit lengthy.
Create a customizer control to add multiple sidebars
The easiest way is to just take the custom control called Multi Input field from here. Also, be sure to add this custom control, and all the jQuery functionality that goes with it (in the .js file).
With that, add to your customizer file (or if you don't have a dedicated customizer file, to your functions.php file)
if(!function_exists('yourtheme_text_sanitization')){
function yourtheme_text_sanitization($input){
return wp_kses_post( force_balance_tags($input) );
}
}
/**
------------------------------------------------------------
SECTION: Sidebars
------------------------------------------------------------
**/
$wp_customize->add_section('section_sidebars', array(
'title' => esc_html__('Sidebars', 'yourtheme'),
'priority' => 0,
));
/**
Sidebars
**/
$wp_customize->add_setting('sidebars', array(
'default' => '',
'sanitize_callback' => 'yourtheme_text_sanitization',
));
$wp_customize->add_control(new Multi_Input_Custom_control($wp_customize, 'sidebars', array(
'label' => esc_html__( 'Sidebars', 'yourtheme' ),
'description' => esc_html__( 'Add as many custom sidebars as you need', 'yourtheme' ),
'settings' => 'sidebars',
'section' => 'section_sidebars',
)));
The first function is the sanitization function for your customizer sanitization callback.
So now you should have 'Sidebars' in your Appearance > Customize menu.
Create sidebars dynamically
But now you need to add your sidebars. If you have a dedicated sidebar file like sidebars.php where you register your theme sidebars, you can put this there, or in your functions.php file
if ( function_exists( 'register_sidebar' ) ) {
$sidebars=get_theme_mod('sidebars','');
if($sidebars!=''){
$sidebars = explode('|', $sidebars);
$i = 0;
foreach($sidebars as $sidebar){
$i++;
register_sidebar(array(
'name'=>$sidebar,
'id' => 'sidebar-'.$i,
'before_widget' => '<div id="%1$s" class="widget %2$s">',
'after_widget' => '</div>',
'before_title' => '<div class="sidebar-widget-heading"><h3>',
'after_title' => '</h3></div>',
));
}
}
}
This is the minimum you need so that you can add multiple sidebars in the theme, and they should appear in Appearance > Widgets.
Add sidebar metabox
Now adding a metabox to display them is relatively easy. In your functions.php, or a separate file where you add metaboxes to posts/pages/CPT, add
// Add metabox
add_action('admin_init', 'yourtheme_post_add_meta');
if ( ! function_exists( 'yourtheme_post_add_meta' ) ){
function yourtheme_post_add_meta(){
add_meta_box('post-sidebar', esc_html__('Select Sidebar', 'yourtheme'), 'yourtheme_post_sidebar_meta_box', 'post');
}
}
//Metabox
if ( ! function_exists( 'yourtheme_post_sidebar_meta_box' ) ){
function yourtheme_post_sidebar_meta_box( $post ){
$values = get_post_custom( $post->ID );
$custom_sidebar = (isset($values['custom_sidebar'][0])) ? $values['custom_sidebar'][0] : '';
wp_nonce_field( 'my_meta_box_sidebar_nonce', 'meta_box_sidebar_nonce' );
?>
<p>
<select name="custom_sidebar" id="custom_sidebar">
<?php
global $wp_registered_sidebars;
$sidebar_replacements = $wp_registered_sidebars;
if(is_array($sidebar_replacements) && !empty($sidebar_replacements)){
foreach($sidebar_replacements as $sidebar){
echo "<option value='{$sidebar['name']}'".selected($sidebar['name'], $custom_sidebar, false).">{$sidebar['name']}</option>";
}
}
?>
</select>
</p>
<?php
}
}
//Save Metabox
add_action( 'save_post', 'yourtheme_post_save_sidebar_meta_box' );
if ( ! function_exists( 'yourtheme_post_save_sidebar_meta_box' ) ){
function yourtheme_post_save_sidebar_meta_box( $post_id ){
if( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ){
return;
}
if( !isset( $_POST['custom_sidebar'] ) || !wp_verify_nonce( $_POST['meta_box_sidebar_nonce'], 'my_meta_box_sidebar_nonce' ) ) {
return;
}
if( !current_user_can( 'edit_pages' ) ) {
return;
}
if( isset( $_POST['custom_sidebar'] ) ){
update_post_meta( $post_id, 'custom_sidebar', wp_kses( $_POST['custom_sidebar'] ,'') );
}
}
}
And this should be it. A bit lengthy, but a way to add as many sidebars to your theme as you wish :)
Hope it helps.
Giving the users option to choose a sidebar is fine, but i dont think its a good idea to give them functionality to create their own sidebars. You will quickly get into the problem where you will have tons of sidebars in your widget area each for a different post.
However if i was in your situation, i would have created a single sidebar and then dynamically added widgets to this sidebar depending on the post that is being viewed. For the end user both will look the same. Anyways there is no right or wrong answer here, it depends on how you want to tackle the situation.
Related
So today, all my websites were updated to the new release of WordPress 5.9.1. Good good. However, my custom blocks in Gutenberg that are containing an image element are breaking the style of the media modal (where you can add an image directly in the post).
I started a new project, just to test if it was my theme, or the plugins, but even without any plugin (except ACF Pro) and on the Twenty Twenty-Two theme, if I add my registration code in the functions.php file of 2022 theme, I get the same problem.
Here's the register-block code:
add_action('acf/init', 'my_acf_init_block_types');
function my_acf_init_block_types() {
if( function_exists('acf_register_block_type') ) {
acf_register_block_type(array(
'name' => 'carousel',
'title' => __('Carrousel'),
'description' => __(''),
'render_template' => 'web/blocks/carousel.php',
'category' => 'custom-blocks',
'icon' => 'images-alt',
'keywords' => array( 'carousel', 'carrousel'),
'supports' => array( 'anchor' => true),
));
}
}
And I've created a Field group trying the image with the array annnnnd the one using only the URL.
What I tried:
no plugins (except ACF)
WP theme (2022)
custom theme with no functions
adding the registration code to 2022 theme (same error)
Please, help a sister our here.
I think it was cause by the 5.9.1 update
You can use this in functions.php as temporary fix
function fix_media_views_css() {
echo '<link rel="stylesheet" id="fix-media-views-css" href="'.get_bloginfo('url').'/wp-includes/css/media-views.min.css?ver=5.9.1" media="all">';
}
add_action('admin_footer', 'fix_media_views_css');
I've added that piece of code to my functions.php file (at the end, no biggy).
function acf_filter_rest_api_preload_paths( $preload_paths ) {
if ( ! get_the_ID() ) {
return $preload_paths;
}
$remove_path = '/wp/v2/' . get_post_type() . 's/' . get_the_ID() . '?context=edit';
$v1 = array_filter(
$preload_paths,
function( $url ) use ( $remove_path ) {
return $url !== $remove_path;
}
);
$remove_path = '/wp/v2/' . get_post_type() . 's/' . get_the_ID() . '/autosaves?context=edit';
return array_filter(
$v1,
function( $url ) use ( $remove_path ) {
return $url !== $remove_path;
}
);
}
add_filter( 'block_editor_rest_api_preload_paths', 'acf_filter_rest_api_preload_paths', 10, 1 );
It works perfectly like before. I've tried to downversion it to 5.9 and it worked as well, but it takes more time/effort and many mistakes can happen.
Hope it helps more than one.
ACF is aware of the issue: https://github.com/AdvancedCustomFields/acf/issues/612
Here is the temp fix, paste in your functions.php:
function acf_filter_rest_api_preload_paths( $preload_paths ) {
global $post;
$rest_path = rest_get_route_for_post( $post );
$remove_paths = array(
add_query_arg( 'context', 'edit', $rest_path ),
sprintf( '%s/autosaves?context=edit', $rest_path ),
);
return array_filter(
$preload_paths,
function( $url ) use ( $remove_paths ) {
return ! in_array( $url, $remove_paths, true );
}
);
}
add_filter( 'block_editor_rest_api_preload_paths', 'acf_filter_rest_api_preload_paths', 10, 1 );
I have a custom post type setup and in the sidebar of every page on the site it is set to display a random post from that custom post type (named "reviews").
This works great everywhere except for category pages for the normal / standard / default post type of "post" where even though the query is setup to only use the custom post type "reviews" it only pulls from the default blog posts.
Is there something I am leaving out to make sure this works even on category pages?
Here is the code I am using that works fine on non category pages, you can see it is restricted to just the "reviews" post type:
// the query
$review_query = new WP_Query( array (
'post_type' => 'reviews', // Display just this post type
'orderby' => 'rand',
'posts_per_page' => 1,
)
);
You can create shortcode that you can put in any widget area you want.
Something like:
function wpb_rand_posts() {
if ( $the_query->have_posts() ) {
$string .= '<ul>';
while ( $the_query->have_posts() ) {
//do stuff
}
$string .= '</ul>';
/* Restore original Post Data */
wp_reset_postdata();
} else {
$string .= 'no posts found';
}
return $string;
}
add_shortcode('wpb-random-posts','wpb_rand_posts');
add_filter('widget_text', 'do_shortcode');
Ok so it turns out the code I had added to functions.php to make sure a different custom post type was displaying in the category feed was interfering.
So this was the original code snip I used to do that:
// Show notable cases in tag archives
function themeprefix_show_cpt_archives( $query ) {
if( is_category() || is_tag() && empty( $query->query_vars['suppress_filters'] ) ) {
$query->set( 'post_type', array(
'nav_menu_item', 'post', 'cases'
));
return $query;
}
}
And I changed that to this:
// Show notable cases in tag archives
function themeprefix_show_cpt_archives( $query ) {
if( empty( $query->query_vars['suppress_filters'] ) && ( is_category() || is_tag() ) ) {
$query->set( 'post_type', array(
'nav_menu_item', 'post', 'cases'
));
return $query;
}
}
And added suppress_filters' => true to my query, and this resolved my issue. If anyone else runs into this see what else in your theme may be modifying the query at a higher level through a function like this or plugin as this is a solution specific to the code in my theme.
I'm using a plugin for wordpress-woocommerce called Woocommerce Cart PDF (https://wordpress.org/plugins/wc-cart-pdf/). It generates a pdf-link of the current cart, but is located on the cart page.
I have a combined cart and checkout page so the link does not appear on my website. I don't have the knowledge on how to edit the plugin files myself for it to appear on my cart page.
I've tried the wordpress plugin support forum for this specific plugin, but no answer.
/**
* Generates the PDF for download
*
* #return void
*/
function wc_cart_pdf_process_download() {
if( ! function_exists( 'WC' ) ) {
return;
}
if( ! isset( $_GET['cart-pdf'] ) ) {
return;
}
if( ! is_cart() || WC()->cart->is_empty() ) {
return;
}
if( ! isset( $_GET['_wpnonce'] ) || ! wp_verify_nonce( $_GET['_wpnonce'], 'cart-pdf' ) ) {
wc_add_notice( __( 'Invalid nonce. Unable to process PDF for download.', 'wc_cart_pdf' ), 'error' );
return;
}
$dompdf = new \Dompdf\Dompdf();
$content = $css = '';
$cart_table = wc_locate_template( 'cart-table.php', '/woocommerce/wc-cart-pdf/', __DIR__ . '/templates/' );
$css = wc_locate_template( 'pdf-styles.php', '/woocommerce/wc-cart-pdf/', __DIR__ . '/templates/' );
do_action( 'wc_cart_pdf_before_process' );
if( file_exists( $cart_table ) ) {
ob_start();
include $cart_table;
$content = ob_get_clean();
}
if( file_exists( $css ) ) {
ob_start();
include $css;
$css = apply_filters( 'woocommerce_email_styles', ob_get_clean() );
}
$dompdf->loadHtml( '<style>' . $css . '</style>' . $content );
$dompdf->setPaper( 'A4', 'portrait' );
$dompdf->render();
$dompdf->stream(
apply_filters( 'wc_cart_pdf_filename', 'WC_Cart-' . date( 'Ymd' ) . bin2hex( openssl_random_pseudo_bytes( 5 ) ) ) . '.pdf',
/**
* 'compress' => 1 or 0 - apply content stream compression, this is on (1) by default
* 'Attachment' => 1 or 0 - if 1, force the browser to open a download dialog, on (1) by default
*/
apply_filters( 'wc_cart_pdf_stream_options', array( 'compress' => 1, 'Attachment' => 1 ) )
);
exit;
}
add_action( 'template_redirect', 'wc_cart_pdf_process_download' );
if( ! function_exists( 'wc_cart_pdf_button' ) ) {
/**
* Renders the download cart as PDF button
*
* #return void
*/
function wc_cart_pdf_button() {
if( ! is_cart() || WC()->cart->is_empty() ) {
return;
}
?>
<a href="<?php echo esc_url( wp_nonce_url( add_query_arg( array( 'cart-pdf' => '1' ), wc_get_cart_url() ), 'cart-pdf' ) );?>" class="cart-pdf-button button" target="_blank">
<?php esc_html_e( 'Download Cart as PDF', 'wc-cart-pdf' ); ?>
</a>
<?php
}
}
add_action( 'woocommerce_proceed_to_checkout', 'wc_cart_pdf_button', 21 );
This might be the incorrect part of the functions in the plugin, but I hop I got it right.
The wc_cart_pdf_process_download() function isn't really relevant. The comment states that it "Generates the PDF for download". What it's doing is responding when the user visits the PDF link by generating the requested PDF file. The important function is the one beneath that, wc_cart_pdf_button().
Now that we know the function we're interested in, what's next? In your question, you suggested editing the plugin files however it's important to avoid doing that. Editing your plugin files is a sure-fire way to guarantee the changes you make get overwritten the next time you update.
You have a couple of options:
Create a mini feature plugin.
Add the code to the bottom of your (hopefully child) theme's functions.php file.
The first option would be the recommended approach but that's going to take us well beyond the scope of the question. Placing the code in a child theme's functions.php file will be adequate for getting you up and running.
Okay, so now we know what the code we want to modify is and where we're going to store those modifications. Let's break down the actual code:
if( ! is_cart() || WC()->cart->is_empty() ) {
return;
}
This checks two things, are we on the cart page and does the cart contain items? If either is false, we're going to bail out early. You're on the checkout page, not the cart page, so even if this function were to be called, it wouldn't make it past this conditional.
<a href="<?php echo esc_url( wp_nonce_url( add_query_arg( array( 'cart-pdf' => '1' ), wc_get_cart_url() ), 'cart-pdf' ) );?>" class="cart-pdf-button button" target="_blank">
<?php esc_html_e( 'Download Cart as PDF', 'wc-cart-pdf' ); ?>
</a>
If those two previous checks passed, generate the button output.
add_action( 'woocommerce_proceed_to_checkout', 'wc_cart_pdf_button', 21 );
This executes the code on the woocommerce_proceed_to_checkout hook which fires after the cart totals on the cart page. The same action is used by the checkout button itself.
We need to write our own function that displays that same output on the checkout page. Without knowing where you'd like the button to appear, I can't suggest which action to use. I'm using woocommerce_checkout_order_review with a priority that'll put it between the order table and the payment options. If you need to reposition it, you'll have to go through those hooks and find somewhere that feels appropriate.
You did mention in your question that this is necessary because you have your cart and checkout pages combined. You may require a completely different hook, again there's no way for me to know based on your question alone.
Here's the final code:
function stackoverflow_wc_checkout_pdf_button() {
// We're on the checkout page based on the action.
// Highly unlikely we need the is_empty() check but it can't hurt if you may find yourself reusing elsewhere.
if ( WC()->cart->is_empty() ) {
return;
} ?>
<a href="<?php echo esc_url( wp_nonce_url( add_query_arg( array( 'cart-pdf' => '1' ), wc_get_cart_url() ), 'cart-pdf' ) );?>" class="cart-pdf-button button" target="_blank">
<?php esc_html_e( 'Download Cart as PDF', 'wc-cart-pdf' ); ?>
</a>
<?php
}
add_action( 'woocommerce_checkout_order_review', 'stackoverflow_wc_checkout_pdf_button', 15 );
When using the searchbar in the "news" page of my Wordpress, it displays posts from the blog but also posts that are products from Woocommerce, or other pages / content. I would like it to display only blog posts.
I haven't found any solution yet. Also, Wordpress won't let me update the php files of the theme, so I think I need on-site settings / plugins.
Here is the news page : https://champagne-oudart.com/actualites/
The client wanted the news categories to be years.
I recommend to use a plugin like Relevanssi https://wordpress.org/plugins/relevanssi/
It enables you to limit/control the search in many ways, also the one you asked for, and it's very simple to handle.
function modify_search_query( $query ) {
// Make sure this isn't the admin or is the main query
if( is_admin() || ! $query->is_main_query() ) {
return;
}
// Make sure this isn't the WooCommerce product search form
if( isset($_GET['post_type']) && ($_GET['post_type'] == 'product') ) {
return;
}
if( $query->is_search() ) {
$in_search_post_types = get_post_types( array( 'exclude_from_search' => false ) );
// The post types you're removing (example: 'product' and 'page')
$post_types_to_remove = array( 'product', 'page' );
foreach( $post_types_to_remove as $post_type_to_remove ) {
if( is_array( $in_search_post_types ) && in_array( $post_type_to_remove, $in_search_post_types ) ) {
unset( $in_search_post_types[ $post_type_to_remove ] );
$query->set( 'post_type', $in_search_post_types );
}
}
}
}
add_action( 'pre_get_posts', 'modify_search_query' );
add these lines to your functions.php
I'm developing a plugin which has its own table. I need to display the data from the table in the Wordpress frontend (for example: category page). I don't need to JOIN this table with posts table, I just need to display the data from the table, with pagination. I need a separate page/custom template from my plugin directory (talking in a context of MVC frameworks — controller), on which this data should be displayed and paginated.
Please give me an advice, what is the best practice to implement it?
Thanks.
If I understood your question then I think you need to add template_include hook to use Custom template/page from your plugin directory and you can do it like
add_filter('template_include', 'my_template', 1, 1);
function my_template($template) {
global $post;
if($post->post_content == '[myPluginPage]')
return dirname(__FILE__) . '/my_pligin_template.php';
return $template;
}
You should paste the code given above in your plugin file and also create a file in your plugin folder with name my_pligin_template.php or whatever you want but in this case in the first return statement you have to change the file name too.
Now you have to create a page in wordpress admin to show the page in the menu bar and just paste [myPluginPage] as the content. Notice if($post->post_content == '[myPluginPage]'), this will check whether it's your plugin page or not whenever you click on any menu item and if it finds the word [myPluginPage] in the content then it will return the custom template and you will be at that page.
Now you need to fetch your data from database and to do it you should write the code in this custom template file (my_pligin_template.php). To do it you can write
global $wpdb;
$pagenum = isset( $_GET['pagenum'] ) ? absint( $_GET['pagenum'] ) : 1;
$limit = 10;
$offset = ($pagenum-1) * $limit;
$total = $wpdb->get_var( "SELECT COUNT(*) FROM yourtable" );
$num_of_pages = ceil( $total / $limit );
$qry="select * from yourtable LIMIT $offset, $limit";
$result=$wpdb->get_results($qry);
if($result):
foreach($result as $row)
{
// code here
}
//Link for Pagination
$page_links = paginate_links( array(
'base' => add_query_arg( 'pagenum', '%#%' ),
'format' => '',
'prev_text' => __( '«', 'aag' ),
'next_text' => __( '»', 'aag' ),
'total' => $num_of_pages,
'current' => $pagenum
) );
if ( $page_links ) {
echo '<div class="tablenav"><div class="tablenav-pages" style="margin: 1em 0">' . $page_links . '</div></div>';
}
endif;