Common WordPress problems and quick fixes
While there are many nice features of the WordPress CMS system, many plugin and theme features may conflict with each other due to its open source code. In addition, although it is a system preferred by millions, the WordPress ready content management system can be weak in special areas and may need manual intervention. This is the real reason why WordPress, which is distributed with open source code, is already one of the most preferred systems worldwide, so that you can produce custom solutions for your own needs.
However, there are some problems that you cannot get rid of without intervention, we will share a few of them with you and try to express the solutions.
Fix WordPress Cannot redeclare get_avatar_url() error
We are adding the archive solution for the Cannot redeclare get_avatar_url() error to use with our WordPress-loving friends to keep it registered based on the articles we researched and the method we used.
You change the codes in the functions.php file to make arrangements for your theme.
Legacy code:
function get_avatar_url( $get_avatar ) { preg_match("/src='(.*?)'/i", $get_avatar, $matches); return $matches[1]; }
New code:
if(!function_exists('get_avatar_url')){ function get_avatar_url( $get_avatar ) { preg_match("/src='(.*?)'/i", $get_avatar); 1TP3 return $matches[1]; } }
Adding views to WordPress post content without using a plugin
You can easily play with WordPress topic views with SQL commands.
While there are thousands of alternative methods to add fake views to your posts, you can add as many numbers as you want with a code added to the functions.php file inside your theme and make up for your deficiencies manually at this point.
In functions.php:
function getPostViews($postID){
$count_key = 'post_views_count';
$count = get_post_meta($postID, $count_key, true);
if($count==''){
delete_post_meta($postID, $count_key);
add_post_meta($postID, $count_key, '0');
return "0 View";
}
return $count.' Views';
}
function setPostViews($postID) {
$count_key = 'post_views_count';
$count = get_post_meta($postID, $count_key, true);
if($count==''){
$count = 0;
delete_post_meta($postID, $count_key);
add_post_meta($postID, $count_key, '0');
}else{
$count++;
update_post_meta($postID, $count_key, $count);
}
}
// Remove issues with prefetching adding extra views
remove_action( 'wp_head', 'adjacent_posts_rel_link_wp_head', 10, 0);
Then single.php and index.php We paste it wherever we want.
<?php
echo getPostViews(get_the_ID());
?>
W3 Total Cache If a similar caching plugin is available, we may need additional code:
I am updating.
In Functions.php:
function getPostViews($postID){
$count_key = 'post_views_count';
$count = get_post_meta($postID, $count_key, true);
if($count==''){
delete_post_meta($postID, $count_key);
add_post_meta($postID, $count_key, '0');
return "0 View";
}
return $count.' Views';
}
function setPostViews($postID) {
$count_key = 'post_views_count';
$count = get_post_meta($postID, $count_key, true);
if($count==''){
$count = 0;
delete_post_meta($postID, $count_key);
add_post_meta($postID, $count_key, '0');
}else{
$count++;
update_post_meta($postID, $count_key, $count);
}
}
Into pages such as single and index:
before:
<?php
echo getPostViews(get_the_ID());
?>
Then:
<?php
setPostViews(get_the_ID());
?>
Adding WordPress Woocommerce Front Page Dropdown Droplist
I know the title is too much english. However, we are already writing to add the system we have solved as a note after spending hours in order not to forget it for archival purposes.
<div class="col-md-3 webfilter" id="profilter">
<div class="prolist col-md-10 errormsg" >
</div>
<div class="prolist col-md-10" >
<input type="hidden" id="ajaxpath" value="<?php echo get_bloginfo('template_directory').'/getproducts.php'; ?>" />
<select name="brand" class="homedrop" id="brand">
<option value="">Manufacturer</option>
<?php
$args=array(
'hide_empty' => 0,
'orderby' => 'name',
'order' => 'ASC',
'hierarchical' => 0,
'style' => 'none',
'taxonomy' => 'product_cat',
'depth' => 0,
'parent' => 0
);
$res = get_categories($args);
foreach($res as $result) {
echo '<option value="'.$result->term_id.'">'.$result->name.'</option>' ;
}
?>
</select>
</div>
<div class="prolist col-md-10">
<select name="model" class="homedrop" id="model">
<option>Model</option>
</select>
</div>
<div class="prolist col-md-10">
<select name="motor" class="homedrop" id="motor">
<option>Engine</option>
</select>
</div>
<div style="color:#FFF !important;" id="findproduct" class="cs-btn-align text-center"><a href="#" style="color:#FFF !important;" class="dropbutton cs-btn cs-btn-outlined avc cs-btn-rounded cs-btn-outlined-accent cs-btn-sm">FIND MY VEHICLE</a></div>
</div>
this code has presented us the image that we will use as a widget on the home page and the optional menu.
Now, with the $category function, let's create a function file to name and retrieve the levels to list all the content on our site.
<?php
require_once( dirname(__FILE__) . '/../../../wp-load.php' );
if($_POST['action'] == 'brand') {
$brandid = $_POST['brand'];
$args=array(
'hide_empty' => 0,
'orderby' => 'name',
'order' => 'ASC',
'hierarchical' => 0,
'style' => 'none',
'taxonomy' => 'product_cat',
'depth' => 0,
'parent' => $brandid
);
$childs = get_categories($args);
$result = array();
$i = 0;
foreach($childs as $child) {
$result[$i]['id'] = $child->term_id;
$result[$i]['name'] = $child->name;
$i++;
}
echo json_encode($result);
die();
}
if($_POST['action'] == 'model') {
$brandid = $_POST['brand'];
$args=array(
'hide_empty' => 0,
'orderby' => 'name',
'order' => 'ASC',
'hierarchical' => 0,
'style' => 'none',
'taxonomy' => 'product_cat',
'depth' => 0,
'parent' => $brandid
);
$childs = get_categories($args);
$result = array();
$i = 0;
$baseurl = get_bloginfo('url');
foreach($childs as $child) {
$result[$i]['id'] = $baseurl.'/product-category/'.$child->slug;
$result[$i]['name'] = $child->name;
$i++;
}
echo json_encode($result);
die();
}
if($_POST['action'] == 'search'){
$args = array(
'post_type' => 'product',
'posts_per_page' => -1
);
$wp_query = new Wp_Query($args);
$posts = $wp_query->posts;
$result = array();
$i = 0;
foreach($posts as $post) {
$result[$i]['label'] = $post->post_title;
$result[$i]['value'] = get_the_permalink($post->ID);
$thumbid = get_post_thumbnail_id($post->ID);
$result[$i]['img'] = wp_get_attachment_image( $thumbid, 'thumbnail' );
if($result[$i]['img'] == '') {
$result[$i]['img'] = '<img width="80" height="80" src="https://dummyimage.com/80x80/8f8f80/fff.png&text=No+image" class="attachment-thumbnail" alt="product-img" />';
}
$i++;
}
//print_r($result); exit;
echo json_encode($result);
die();
}
?>
We have defined the structure, but I need to add a file of the brand application that I have converted within itself for taxonomy consistency.
Therefore, I create the following file in the same way as in the getproducts.php file and open it to the theme main directory.
<?php
/**
*
* Template for Brand taxonomy
* @since 1.0.0
* @version 1.0.1
*
*/
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
$term = get_queried_object();
$parent_id = empty( $term->term_id ) ? 0 : $term->term_id;
$product_categories = get_categories( apply_filters( 'woocommerce_product_subcategories_args', array(
'parent' => $parent_id,
'menu_order' => 'ASC',
'hide_empty' => 0,
'hierarchical' => 1,
'taxonomy' => 'product_cat',
'pad_counts' => 1
) ) );
if ( $product_categories ) {
get_header( 'shop' );
get_template_part( 'templates/page-header' );
$cs_post_meta = get_post_meta( $post->ID, '_custom_page_options', true );
$cs_page_layout = ( isset ( $cs_post_meta['sidebar'] ) ) ? $cs_post_meta['sidebar'] : 'full';
$cs_page_column = ( $cs_page_layout == 'full' ) ? '12' : '9';
$vc_use_default = cs_get_option( 'vc_use_default' );
$cs_page_padding = ( $vc_use_default !== true ) ? 'md-padding' : '';
if( ( $cs_page_layout == 'fluid' || isset( $cs_post_meta['section'] ) ) && $vc_use_default !== true ) {
get_template_part('templates/page', 'section');
} else { ?>
<section class="main-content <?php echo $cs_page_padding; ?> page-layout-<?php echo $cs_page_layout; ?>">
<div class="container">
<div class="row">
<?php cs_page_sidebar( 'left', $cs_page_layout ); ?>
<div class="col-md-<?php echo $cs_page_column; ?> cs-sidebar-clear">
<div class="page-content">
<?php foreach ( $product_categories as $category ) {
$category_link = get_category_link( $category );
echo '<a href="'. esc_url( $category_link ).' "> <div class="col-md-2 b-tab" attr="'.$category->cat_ID.'" >'.$category->name.'</div> </a>';
}
?>
</div>
</div>
<?php cs_page_sidebar( 'right', $cs_page_layout ); ?>
</div>
</div>
</section>
<?php }
get_footer();
}else{
wc_get_template( 'archive-product.php' );
}
If we have a page such as a custom listing (find my vehicle), we change the template.
<?php
/**
*
* Template Name: Brand
* @since 1.0.0
* @version 1.0.1
*
*/
get_header();
get_template_part( 'templates/page-header' );
$cs_post_meta = get_post_meta( $post->ID, '_custom_page_options', true );
$cs_page_layout = ( isset ( $cs_post_meta['sidebar'] ) ) ? $cs_post_meta['sidebar'] : 'full';
$cs_page_column = ( $cs_page_layout == 'full' ) ? '12' : '9';
$vc_use_default = cs_get_option( 'vc_use_default' );
$cs_page_padding = ( $vc_use_default !== true ) ? 'md-padding' : '';
if( ( $cs_page_layout == 'fluid' || isset( $cs_post_meta['section'] ) ) && $vc_use_default !== true ) {
get_template_part('templates/page', 'section');
} else {
?>
<section class="main-content <?php echo $cs_page_padding; ?> page-layout-<?php echo $cs_page_layout; ?>">
<div class="container">
<div class="row">
<?php cs_page_sidebar( 'left', $cs_page_layout ); ?>
<div class="col-md-<?php echo $cs_page_column; ?> cs-sidebar-clear">
<div class="page-content">
<?php
$args=array(
'hide_empty' => 0,
'orderby' => 'name',
'order' => 'ASC',
'taxonomy' => 'product_cat',
'depth' => 0,
'parent' => 0
);
$res = get_categories($args);
foreach($res as $result) {
$category_link = get_category_link( $result );
echo '<a href="'. esc_url( $category_link ).' "> <div class="col-md-2 b-tab" attr="'.$result->cat_ID.'" >'.$result->name.'</div> </a>';
}
?>
</div>
</div>
<?php cs_page_sidebar( 'right', $cs_page_layout ); ?>
</div>
</div>
</section>
<?php
}
get_footer();
Redirecting the site to https and 301 with htaccess after SSL activation
You have a site, you set it up with http, you are using mysql and php.
You have purchased an SSL certificate or you want to redirect to https with a Let's Encrypt style system.
Especially if you are a WordPress user and have tried dozens of plugins, you have struggled in vain.
RewriteCond %{HTTPS} off # First rewrite to HTTPS: # Don't put www. here. If it is already there it will be included, if not # the subsequent rule will catch it. RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301] # Now, rewrite any request to the wrong domain to use www. # [NC] is a case-insensitive match RewriteCond %{HTTP_HOST} !^www. [NC] RewriteRule .* https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
URL are not cached by some proxy caching servers error and solution
You have used many cache systems, still "?" Are you having trouble with .css and .js files with the extension?
Because files with this variable are not static, therefore caching cannot be done.
However, the code below is functions.php you place it on the bottom line and bingo.
/** * Remove Query strings from Static Resources. */ function remove_cssjs_ver( $src ) { if( strpos( $src, '?ver=' ) ) $src = remove_query_arg( 'ver', $src ); return $src; } add_filter( 'style_loader_src', 'remove_cssjs_ver', 10, 2 ); add_filter( 'script_loader_src', 'remove_cssjs_ver', 10, 2 );
Quick solution for WordPress and Woocommerce slowness problem
As a Woocommerce user, if you have thousands of product ranges, unfortunately, you install many plugins for your cashier system and try to find a solution with Cache systems.
However, since Woocommerce is a system that appeals to the whole world, it is a system that compulsorily runs many unnecessary functions simultaneously. To alleviate this load, you can lighten the distribution of functions that you deem unnecessary, so that the features you do not use are not called by the system and save you speed / time.
Just add the code I have given below to your theme's functions.php file, then you can delete the lines you want or add others. I'm sure you will gain a noticeable speed this way!
add_action( 'wp_enqueue_scripts', 'child_manage_woocommerce_styles', 99 ); function child_manage_woocommerce_styles() { //remove generator meta tag remove_action( 'wp_head', array( $GLOBALS['woocommerce'], 'generator' ) ); //first check that woo exists to prevent fatal errors if ( function_exists( 'is_woocommerce' ) ) { //dequeue scripts and styles if ( ! is_woocommerce() && ! is_cart() && ! is_checkout() ) { wp_dequeue_style( ' woocommerce_frontend_styles' ); wp_dequeue_style( 'woocommerce_fancybox_styles' ); wp_dequeue_style( 'woocommerce_chosen_styles' ); wp_dequeue_style( 'woocommerce_prettyPhoto_css' ); wp_dequeue_script( 'wc_price_slider' ); wp_dequeue_script( 'wc-single-product' ); wp_dequeue_script( 'wc-add-to-cart' ); wp_dequeue_script( 'wc-cart-fragments' ); wp_dequeue_script( 'wc-checkout' ); wp_dequeue_script( 'wc-add-to-cart-variation' ); wp_dequeue_script( 'wc-single-product' ); wp_dequeue_script( 'wc-cart' ); wp_dequeue_script( 'wc-chosen' ); wp_dequeue_script( 'woocommerce' ); wp_dequeue_script( 'prettyPhoto' ); wp_dequeue_script( 'prettyPhoto-init' ); wp_dequeue_script( 'jquery-blockui' ); wp_dequeue_script( 'jquery-placeholder' ); wp_dequeue_script( 'fancybox' ); wp_dequeue_script( 'jqueryui' ); } } }
Adjust mobile component visibility with WordPress and CSS
Since the responsive logic emerged, mobile visibility has started to emerge as one of the most sought after features. At the point where my amateur theme writer friends are thinking about how we can easily handle the options they look for, such as appear on mobile or appear on desktop, you can eliminate this problem by editing a css file with a simple or two code.
For example, let's code over advertising.
<div class="reklamalani">
<!-- reklam kodunu buraya ekleyin -->
</div>
/* don't show on mobile */ @media (max-width:992px){ .reklamalani{display:none} }
Thus, while your ad code that you place in the ad space will be visible on the desktop, it will be invisible on mobile, more precisely on the 992px screen and below.