Fixing/hardening add_query_arg() usage in WP function - php

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?

Related

Modify php function get_custom_logo to return random images

I'm very inexperienced with coding, so to anyone who bears with me a huge thank you in advance!
Here's what I'm trying to do: I want to make my website - that's currently displaying a logo that I've picked - display randomly one of the images that I've uploaded as the logo every time the page reloads.
Now this would be relatively easy if the theme was coded with a header - but for logos I can't find a plugin that does that and also can't manage to add this feature from inside the theme.
This is how the wordpress theme of my website calls for a logo:
<?php
if( $show_logo && has_custom_logo() ) {
the_custom_logo();
Here's the the_custom_logo() functions code:
function the_custom_logo( $blog_id = 0 ) {
echo get_custom_logo( $blog_id );
}
Now my thought for a solution was the following: I change the way wordpress returns an image through get_custom_logo() (I'm prepared to code that in every time there's a WP update).
Here's the functions code:
function get_custom_logo( $blog_id = 0 ) {
$html = '';
$switched_blog = false;
if ( is_multisite() && ! empty( $blog_id ) && (int) $blog_id !== get_current_blog_id() ) {
switch_to_blog( $blog_id );
$switched_blog = true;
}
$custom_logo_id = get_theme_mod( 'custom_logo' );
// We have a logo. Logo is go.
if ( $custom_logo_id ) {
$html = sprintf( '%2$s',
esc_url( home_url( '/' ) ),
wp_get_attachment_image( $custom_logo_id, 'full', false, array(
'class' => 'custom-logo',
'itemprop' => 'logo',
) )
);
}
// If no logo is set but we're in the Customizer, leave a placeholder (needed for the live preview).
elseif ( is_customize_preview() ) {
$html = sprintf( '<img class="custom-logo"/>',
esc_url( home_url( '/' ) )
);
}
if ( $switched_blog ) {
restore_current_blog();
}
return apply_filters( 'get_custom_logo', $html, $blog_id );
}
Am I wrong in thinking that if I change $custom_logo_id = get_theme_mod( 'custom_logo' ); to acquire a random image out of my logo folder that it's gonna work? And if I'm not wrong, can anybody help me with how to write that piece of code?
I apologise if this is a dumb question. Have a great week!

Wp theme error PHP

Hello guys i got new WP theme and i cant install im getting this error on website
Parse error: syntax error, unexpected '}', expecting end of file in
/storage/h3/665/790665/public_html/wp-content/themes/gameaddict/post_templates.php
on line 1
<?php
class Single_Post_Template_Plugin {
function __construct() {
add_action( 'admin_menu', array( $this, 'add_metabox' ) );
add_action( 'save_post', array( $this, 'metabox_save' ), 1, 2 );
add_filter( 'single_template', array( $this, 'get_post_template' ) );
}
function get_post_template( $template ) {
global $post;
$custom_field = get_post_meta( $post->ID, '_wp_post_template', true );
if( !$custom_field )
return $template;
/** Prevent directory traversal */
$custom_field = str_replace( '..', '', $custom_field );
if( file_exists( get_stylesheet_directory() . "/{$custom_field}" ) )
$template = get_stylesheet_directory() . "/{$custom_field}";
elseif( file_exists( get_template_directory() . "/{$custom_field}" ) )
$template = get_template_directory() . "/{$custom_field}";
return $template;
}
function get_post_templates() {
$templates = wp_get_theme()->get_files( 'php', 1 );
$post_templates = array();
$base = array( trailingslashit( get_template_directory()), trailingslashit( get_stylesheet_directory()) );
foreach ( (array) $templates as $file => $full_path ) {
if( $full_path == get_theme_root().'/gameaddict/post_templates.php'){continue;}else{
if ( !preg_match( '|Single Post Template:(.*)$|mi', file_get_contents( $full_path ), $header ))
continue;
$post_templates[ $file ] = _cleanup_header_comment( $header[1] );
}}
return $post_templates;
}
function post_templates_dropdown() {
global $post;
$post_templates = $this->get_post_templates();
/** Loop through templates, make them options */
foreach ( (array) $post_templates as $template_file => $template_name ) {
$selected = ( $template_file == get_post_meta( $post->ID, '_wp_post_template', true ) ) ? ' selected="selected"' : '';
$opt = '<option value="' . esc_attr( $template_file ) . '"' . $selected . '>' . esc_html( $template_name ) . '</option>';
echo $opt;
}
}
function add_metabox() {
$screens = array( 'post', 'portfolio' );
foreach ( $screens as $screen ) {
add_meta_box(
'pt_post_templates',
__( 'Sidebar position', 'addict' ),
array( $this, 'metabox' ),
$screen,'normal', 'high'
);
}
}
function metabox( $post ) {
?>
<input type="hidden" name="pt_noncename" id="pt_noncename" value="<?php echo wp_create_nonce( plugin_basename( __FILE__ ) ); ?>" />
<label class="hidden" for="post_template"><?php _e( 'Post Template', 'addict' ); ?></label><br />
<select name="_wp_post_template" id="post_template" class="dropdown">
<?php $this->post_templates_dropdown(); ?>
</select>
<?php
}
function metabox_save( $post_id, $post ) {
/*
* Verify this came from the our screen and with proper authorization,
* because save_post can be triggered at other times
*/
if(isset($_POST['pt_noncename'])){
if ( !wp_verify_nonce( $_POST['pt_noncename'], plugin_basename( __FILE__ ) ) )
return $post->ID;
}
/** Is the user allowed to edit the post or page? */
if(isset($_POST['post_type'])){
if ( 'page' == $_POST['post_type'] )
if ( !current_user_can( 'edit_page', $post->ID ) )
return $post->ID;
else
if ( !current_user_can( 'edit_post', $post->ID ) )
return $post->ID;
}
/** OK, we're authenticated: we need to find and save the data */
/** Put the data into an array to make it easier to loop though and save */
if(isset($_POST['_wp_post_template'])){
$mydata['_wp_post_template'] = $_POST['_wp_post_template'];
}
/** Add values of $mydata as custom fields */
if(isset($mydata)){
foreach ( $mydata as $key => $value ) {
/** Don't store custom data twice */
if( 'revision' == $post->post_type )
return;
/** If $value is an array, make it a CSV (unlikely) */
$value = implode( ',', (array) $value );
/** Update the data if it exists, or add it if it doesn't */
if( get_post_meta( $post->ID, $key, false ) )
update_post_meta( $post->ID, $key, $value );
else
add_post_meta( $post->ID, $key, $value );
/** Delete if blank */
if( !$value )
delete_post_meta( $post->ID, $key );
}}
}
}
add_action( 'init', 'post_templates_plugin_init' );
/**
* Instantiate the class after theme has been set up.
*/
function post_templates_plugin_init() {
new Single_Post_Template_Plugin;
}
Assuming the theme is this one https://themeforest.net/item/game-addict-clan-war-gaming-theme/6771881 and it's purchased from themeforest, it should work right out of the box. Try downloading it again, make sure to check if there is not a zipped file inside of the zip you are downloading to make sure you are uploading the right file, sometimes the zip you download has a lot of files like documentation and demos, but the actual theme file is another zip inside.
If nothing of the above helps, write to their customer support.
Basically, it's complaining that wp-content/themes/gameaddict/post_templates.php has a } on line 1, while PHP was expecting end of a file. The code you've included seems fine. The issue is most likely about the developer closing a bracket without opening it, somewhere in the code. It seems like you're using a pre-made theme, in that case contacting the developer would be the best option.

Changing WordPress search URL

this is the code which I'm trying
function search_url_rewrite() {
if ( is_search() && ! empty( $_GET['s'] ) ) {
global $opt; if($opt['rewrite_slug_search']){ $search_url = $opt['rewrite_slug_search'];
} else { $search_url = 'search'; }
wp_redirect( home_url( $search_url ) . urlencode( get_query_var( 's' ) ) );
exit();
}
}
add_action( 'template_redirect', 'search_url_rewrite' );
the $search_url is option from my theme settings panel but there is a problem with the slash when I type test in search box I get searchtest instead of search/test do you have any idea how to get a slash between?

Is there anyway to add rel="next"/rel="prev" into "Page Template" with WordPress SEO by Yoast?

Our site is currently using "WordPress SEO by Yoast"
rel="next" and rel="prev" is working fine on category and archive page, however in page template that we created, rel="next" and rel="prev" is not showing.
(This page template also has pagination)
Our website structure => we have "Article" Post type
in Article we have category
Credit card
Cash card
Loan
etc.
As I want the url to be www.sitename.com/loan without having ../category/loan
I created 'Page' called 'Loan' and using page-loan.php as page template to query Post type 'Article' category 'Loan'
I want to have rel="next" and rel="prev" appear in this page template as well
I wonder is there anyway to use WordPress SEO by Yoast to do it?
or is there anyway to modify below script in the plugin to make rel="next" and rel="prev" appear in Page template as well?
the script I found in the plugin
public function adjacent_rel_links() {
// Don't do this for Genesis, as the way Genesis handles homepage functionality is different and causes issues sometimes.
/**
* Filter 'wpseo_genesis_force_adjacent_rel_home' - Allows devs to allow echoing rel="next" / rel="prev" by WP SEO on Genesis installs
*
* #api bool $unsigned Whether or not to rel=next / rel=prev
*/
if ( is_home() && function_exists( 'genesis' ) && apply_filters( 'wpseo_genesis_force_adjacent_rel_home', false ) === false ) {
return;
}
global $wp_query;
if ( ! is_singular() ) {
$url = $this->canonical( false, true, true );
if ( is_string( $url ) && $url !== '' ) {
$paged = get_query_var( 'paged' );
if ( 0 == $paged ) {
$paged = 1;
}
if ( $paged == 2 ) {
$this->adjacent_rel_link( 'prev', $url, ( $paged - 1 ), true );
}
// Make sure to use index.php when needed, done after paged == 2 check so the prev links to homepage will not have index.php erroneously.
if ( is_front_page() ) {
$url = wpseo_xml_sitemaps_base_url( '' );
}
if ( $paged > 2 ) {
$this->adjacent_rel_link( 'prev', $url, ( $paged - 1 ), true );
}
if ( $paged < $wp_query->max_num_pages ) {
$this->adjacent_rel_link( 'next', $url, ( $paged + 1 ), true );
}
}
}
else {
$numpages = 0;
if ( isset( $wp_query->post->post_content ) ) {
$numpages = ( substr_count( $wp_query->post->post_content, '<!--nextpage-->' ) + 1 );
}
if ( $numpages > 1 ) {
$page = get_query_var( 'page' );
if ( ! $page ) {
$page = 1;
}
$url = get_permalink( $wp_query->post->ID );
// If the current page is the frontpage, pagination should use /base/
if ( $this->is_home_static_page() ) {
$usebase = true;
}
else {
$usebase = false;
}
if ( $page > 1 ) {
$this->adjacent_rel_link( 'prev', $url, ( $page - 1 ), $usebase, 'single_paged' );
}
if ( $page < $numpages ) {
$this->adjacent_rel_link( 'next', $url, ( $page + 1 ), $usebase, 'single_paged' );
}
}
}
}
/**
* Get adjacent pages link for archives
*
* #since 1.0.2
*
* #param string $rel Link relationship, prev or next.
* #param string $url the un-paginated URL of the current archive.
* #param string $page the page number to add on to $url for the $link tag.
* #param boolean $incl_pagination_base whether or not to include /page/ or not.
*
* #return void
*/
private function adjacent_rel_link( $rel, $url, $page, $incl_pagination_base ) {
global $wp_rewrite;
if ( ! $wp_rewrite->using_permalinks() ) {
if ( $page > 1 ) {
$url = add_query_arg( 'paged', $page, $url );
}
}
else {
if ( $page > 1 ) {
$base = '';
if ( $incl_pagination_base ) {
$base = trailingslashit( $wp_rewrite->pagination_base );
}
$url = user_trailingslashit( trailingslashit( $url ) . $base . $page );
}
}
/**
* Filter: 'wpseo_' . $rel . '_rel_link' - Allow changing link rel output by WP SEO
*
* #api string $unsigned The full `<link` element.
*/
$link = apply_filters( 'wpseo_' . $rel . '_rel_link', '<link rel="' . esc_attr( $rel ) . '" href="' . esc_url( $url ) . "\" />\n" );
if ( is_string( $link ) && $link !== '' ) {
echo $link;
}
}
First of all, you have to make a backup of your database. You can achieve this on phpmyadmin or with some plugin that do the backup for you.
Next, you should do the url thing. You can remove the category path from categories in the page "Search Appearance - Yoast SEO", click on tab Taxonomies, then remove the Category URLs.
On the pagination issue, you could manually input the links on each page or use a php function to get all the links of post type articles like this:
wp_list_pages(array(
'child_of' => $post->post_parent,
'exclude' => $post->ID));

Wordpress rewrite with php

Is possible change this url
www.12345.com/?s=&cp_state=city
to this?
www.12345.com/city
Already try with htaccess but in wordpress it doesen't work
Hi have this code. Is possible change it to have a url like this www.12345.com/city?
add_action('template_redirect', 'search_url_rewrite_rule');
function search_url_rewrite_rule() {
global $wp_rewrite;
if ( is_search() && isset( $_GET['s'] ) ) {
$s = str_replace( array( ' ', '%20' ), '+', get_query_var( 's' ) );
wp_redirect( home_url( $wp_rewrite->search_base . '/' . remove_accents ( $s ) ) );
exit();
}
}
add_action('init','change_search_permalinks');
function change_search_permalinks( ) {
global $wp_rewrite;
$wp_rewrite->search_base = 'search';
}
Thank you in advance
In php level it is not good idea
You can use url shortlink in admin area of wordpress.

Categories