Wordpress PHP dynamic sidebar generating a "1" after the code? - php

I am using the following code to display widgets by permalinks...
<?php
global $post;
$permalink = get_permalink( $post->ID );
if ( strpos( $permalink, '/one/' ) !== false || strpos( $permalink, '/one-two/' ) !== false ) {
echo dynamic_sidebar( 'disclaimer_cop' );
} elseif ( strpos( $permalink, '/two/' ) !== false || strpos( $permalink, '/two-one/' ) !== false ) {
echo dynamic_sidebar( 'disclaimer' );
} ?>
This code is dynamically displaying the widgets on the correct pages but when viewing the page the widget position is being followed by a random "1" in the HTML, like this.
<div class="widget_text disclaimer"> <div class="textwidget custom-html-widget">*Disclaimer text.</div>
1
Any ideas as to what is wrong?

Remove the echo. Looks like you're outputting the result as well as the content.
<?php
global $post;
$permalink = get_permalink( $post->ID );
if ( strpos( $permalink, '/one/' ) !== false || strpos( $permalink, '/one-two/' ) !== false ) {
dynamic_sidebar( 'disclaimer_cop' );
} elseif ( strpos( $permalink, '/two/' ) !== false || strpos( $permalink, '/two-one/' ) !== false ) {
dynamic_sidebar( 'disclaimer' );
}
?>

I am not sure how you are implentening on your case the code you specified. If you do it via add_shortcode() and then you call your shortcode on a widget you will just need to change the echo with a return.
Hope that helps.
Cheers!

Related

How to change custom post type permalink?

In my wordpress website :
I need to modify permalink for the custom post type Rivista Quadrimestrale.
Now the permalink is :
https://www.sicilianmarketing.it/diritticomparati/rivista-trimestrale/%postname%
I need it to be
https://www.sicilianmarketing.it/diritticomparati/%postname%
Please help in this case.
You can do it in apache or nginx if needed. You need to give the information to the browser by giving a 301 status code and the new link if called on the old.
https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/301
In your functions.php try adding
function cpt_remove_slug( $post_link, $post, $leavename ) {
if ( 'rivista-trimestrale' != $post->post_type || 'publish' != $post->post_status ) { return $post_link; }
$post_link = str_replace( '/' . $post->post_type . '/', '/', $post_link );
return $post_link;
}
add_filter( 'post_type_link', 'cpt_remove_slug', 10, 3 );
// Removes the slug
function cpt_parse_request( $query ) {
if ( ! $query->is_main_query() || 2 != count( $query->query ) || ! isset( $query->query['page'] ) ) { return; }
if ( ! empty( $query->query['name'] ) ) { $query->set( 'post_type', array( 'post', 'rivista-trimestrale', 'page' ) ); }
}
add_action( 'pre_get_posts', 'cpt_parse_request' );

WordPress show posts within while() loop based on condition

I have 8 posts for example.
Here 3 posts out of 8 posts is sticky and 2 posts has paid_placement meta key out of those 3 posts
Now, within while loop I want to show:
1) those posts on top which has is_sticky() === true and don't have any paid_placement meta key.
2) After that those posts which has is_sticky() === true and have paid_placement meta key.
2) After that custom post type posts ( pro_event as post type).
3) after that all other posts
Now, I am very much confused how to make it happend in WordPress while() loop. Can you guys help me?
My code:
while ( have_posts() ) {
the_post();
if ( is_sticky() && false === ( 'paid' == strtolower( get_post_meta( get_the_ID(), 'paid_placement')[0] ) ) ) {
the_title();
echo '<hr/>';
continue;
}
the_title();
echo '<hr/>';
}
Updated:
while ( have_posts() ) {
the_post();
if ( is_sticky() && false === ( 'paid' == strtolower( get_post_meta( get_the_ID(), 'paid_placement')[0] ) ) ) {
//the_title();
var_dump('paid' == strtolower( get_post_meta( get_the_ID(), 'paid_placement')[0] ) );
echo '<hr/>';
continue;
}
//the_title();
var_dump('paid' == strtolower( get_post_meta( get_the_ID(), 'paid_placement')[0] ) );
echo '<hr/>';
}
output:
bool(true)
bool(false)
bool(true)
bool(false)
bool(false)
bool(false)
bool(false)

Custom meta box for page template not saving data

Been banging my head against the wall trying to figure out the solution to my current issue. Simply put, I've created a 'page-home.php' template page, can get a meta box to call on the page template, but trying to update the page with data in the meta box makes the data disappear.
Code below:
function page_add_meta_boxes( $post ) {
global $post;
if(!empty($post))
// get the page template post meta
$page_template = get_post_meta( $post->ID, '_wp_page_template', true );
// if the current page uses our specific template, then output our custom metabox
{
$pageTemplate = get_post_meta($post->ID, '_wp_page_template', true);
// looks for page-home.php file to add our meta box
if($pageTemplate == 'page-home.php' )
{
add_meta_box(
'page-custom-metabox', // $id
'Special Post Meta', // $title
'page_template_metabox', // $callback
'page', // $page
'normal', // $context
'high'); // $priority
}
}
}
add_action( 'add_meta_boxes_page', 'page_add_meta_boxes' );
function page_template_metabox() {
wp_nonce_field( basename( __FILE__ ), 'page_meta_box_nonce' );
$some_string = get_post_meta( $post->ID, '_some_string', true );
?>
<input type="text" name="some-string" value="<?php echo $some_string; ?>" />
<?php
}
function page_save_custom_post_meta() {
if ( !isset( $_POST['page_meta_box_nonce'] ) || !wp_verify_nonce( $_POST['page_meta_box_nonce'], basename( __FILE__ ) ) ){
return;
}
// return if autosave
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ){
return;
}
// check the user's permissions.
if ( ! current_user_can( 'edit_page', $post_id ) ){
return;
}
// save our string
if ( isset( $_REQUEST['some-string'] ) ) {
update_post_meta( $post_id, '_some_string', sanitize_text_field( $_POST['some-string'] ) );
}
}
add_action( 'publish_page', 'page_save_custom_post_meta' );
add_action( 'draft_page', 'page_save_custom_post_meta' );
add_action( 'future_page', 'page_save_custom_post_meta' );
Any suggestions are appreciated.
Howdy anyone who runs into this in the future. I was able to get it to work with the code below, someone might be able to follow up with a better answer as to why it works. My thinking is that updating my code to 'save_post_page' instead of just 'save_post' makes the difference. (Note I only changed some info due to trying to test it in my theme):
// Add meta box
function frontpage_meta_boxes( $post ){
global $post;
if(!empty($post))
$page_template = get_post_meta( $post->ID, '_wp_page_template', true );
{
$pageTemplate = get_post_meta($post->ID, '_wp_page_template', true);
if($pageTemplate == 'page-home.php' )
{
add_meta_box( 'frontpage_meta_box', __( 'Features' ), 'frontpage_meta_box', 'page', 'advanced', 'high' );
}
}
}
add_action( 'add_meta_boxes_page', 'frontpage_meta_boxes' );
// builds our meta box
function frontpage_meta_box( $post ){
// make sure the form request comes from WordPress
wp_nonce_field( basename( __FILE__ ), 'frontpage_meta_box_nonce' );
// retrieve the _manuf_url current value
$manufacturer_url = get_post_meta( $post->ID, '_manuf_url', true );
?>
<h3><?php _e( 'Manufacturer URL' ); ?></h3>
<p>
<input type="text" name="manufacturer-url" value="<?php echo $manufacturer_url; ?>" />
</p>
<?php
}
// saves our data
function frontpage_save_meta_box_data( $post_id ){
// verify meta box nonce
if ( !isset( $_POST['frontpage_meta_box_nonce'] ) || !wp_verify_nonce( $_POST['frontpage_meta_box_nonce'], basename( __FILE__ ) ) ){
return;
}
// return if autosave
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ){
return;
}
// Check the user's permissions.
if ( ! current_user_can( 'edit_post', $post_id ) ){
return;
}
// manufacturer url string
if ( isset( $_REQUEST['manufacturer-url'] ) ) {
update_post_meta( $post_id, '_manuf_url', sanitize_text_field( $_POST['manufacturer-url'] ) );
}
// store custom fields values
}
add_action( 'save_post_page', 'frontpage_save_meta_box_data' );
Try with below code :
function page_save_custom_post_meta($postid) {
if ( !isset( $_POST['page_meta_box_nonce'] ) || !wp_verify_nonce( $_POST['page_meta_box_nonce'], basename( __FILE__ ) ) ){
return;
}
// return if autosave
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ){
return;
}
// check the user's permissions.
if ( ! current_user_can( 'edit_page', $post_id ) ){
return;
}
// save our string
if ( isset( $_REQUEST['some-string'] ) ) {
update_post_meta( $post_id, '_some_string', sanitize_text_field( $_POST['some-string'] ) );
}
}
add_action('save_post ','page_save_custom_post_meta');

Add CSS Style to Wordpress Sidebar on Article Page

I need to add a unique CSS style to the articles and single pages sidebar on my site to diferenciate from the home page. The sidebar already has a style called ".sidebar" and it works when I change it, but it affects all the site sidebars.
I've been looking at the sidebars.php file and see there are "if" lines for home or single pages, but don't know what to change. Keep in mind I'm not a programmer.
The code I think should be modified is (sidebar.php):
<aside class="sidebar">
<?php
wp_reset_query();
if ( is_home() ){
$sidebar_home = tie_get_option( 'sidebar_home' );
if( $sidebar_home )
dynamic_sidebar ( sanitize_title( $sidebar_home ) );
else dynamic_sidebar( 'primary-widget-area' );
}elseif( is_page() ){
global $get_meta;
$tie_sidebar_pos = $get_meta["tie_sidebar_pos"][0];
if( $tie_sidebar_pos != 'full' ){
$tie_sidebar_post = sanitize_title($get_meta["tie_sidebar_post"][0]);
$sidebar_page = tie_get_option( 'sidebar_page' );
if( $tie_sidebar_post )
dynamic_sidebar($tie_sidebar_post);
elseif( $sidebar_page )
dynamic_sidebar ( sanitize_title( $sidebar_page ) );
else dynamic_sidebar( 'primary-widget-area' );
}
}elseif ( is_single() ){
global $get_meta;
$tie_sidebar_pos = $get_meta["tie_sidebar_pos"][0];
if( $tie_sidebar_pos != 'full' ){
$tie_sidebar_post = sanitize_title($get_meta["tie_sidebar_post"][0]);
$sidebar_post = tie_get_option( 'sidebar_post' );
if( $tie_sidebar_post )
dynamic_sidebar($tie_sidebar_post);
elseif( $sidebar_post )
dynamic_sidebar ( sanitize_title( $sidebar_post ) );
else dynamic_sidebar( 'primary-widget-area' );
}
}elseif ( is_category() ){
$category_id = get_query_var('cat') ;
$cat_sidebar = tie_get_option( 'sidebar_cat_'.$category_id ) ;
$sidebar_archive = tie_get_option( 'sidebar_archive' );
if( $cat_sidebar )
dynamic_sidebar ( sanitize_title( $cat_sidebar ) );
elseif( $sidebar_archive )
dynamic_sidebar ( sanitize_title( $sidebar_archive ) );
else dynamic_sidebar( 'primary-widget-area' );
}else{
$sidebar_archive = tie_get_option( 'sidebar_archive' );
if( $sidebar_archive ){
dynamic_sidebar ( sanitize_title( $sidebar_archive ) );
}
else dynamic_sidebar( 'primary-widget-area' );
}
?>
</aside>
Thanks in advance for any help solving this.
your article should have a unique page or post id, have a look at the page/post source code to get the id. Your css will look something like #post_455 .sidebar {
/* your unique article styles */
}
ps - you shouldn't need to play around with sidebar.php

Fixing/hardening add_query_arg() usage in WP function

According to this recent post by the WordPress team, we need to check on our theme files to see if it's using the add_query_arg() and remove_query_arg() functions. Having done a full search of our current theme, I can see a reference to add_query_arg() in this snippet in our pagination.php file:
Here is PHP Code as follows :
if ( !function_exists( 'get_multipage_link' ) ) :
function get_multipage_link( $page = 1 ) {
global $post, $wp_rewrite;
if ( 1 == $page ) {
$url = get_permalink();
} else {
if ( '' == get_option('permalink_structure') || in_array( $post->post_status, array( 'draft', 'pending') ) )
add_query_arg( 'page', $page, get_permalink() );
elseif ( 'page' == get_option( 'show_on_front' ) && get_option('page_on_front') == $post->ID )
$url = trailingslashit( get_permalink() ) . user_trailingslashit( $wp_rewrite->pagination_base . "/$page", 'single_paged' );
else
$url = trailingslashit( get_permalink() ) . user_trailingslashit( $page, 'single_paged' );
}
return $url;
}
endif;
Unfortunately, I'm a bit out of my depth here. Is that reference OK as it is referencing get_permalink() and not a URL? That's way I don't need to use the esc_url() that the blog posting recommends?

Categories