Skip to Content
PHPCODE
phpcodeinformation WordPress Standard
wordpress code / April 15, 2022

phpcodeinformation WordPress Standard

 

Create theme from “http://underscores.me” with proper name and comments

Theme Name: SITENAME

Theme URI: SITEURL

Author: SITENAME

Author URI: SITEURL

Description: SITENAME

Version: 1.0

License: GNU General Public License v2 or later

License URI: http://www.gnu.org/licenses/gpl-2.0.html

Text Domain: siteslug

 

Add below code in “wp-config.php” after “define(‘WP_DEBUG’, false);

define( ‘DISALLOW_FILE_EDIT’, true );

define(‘WP_POST_REVISIONS’, false);

 

Add below filter in general_function.php for hiding login error message

function inn_wrong_login() {

return ‘Wrong username or password.’;

}

add_filter(‘login_errors’, ‘inn_wrong_login’);

 

Add below code in “functions.php

remove_action(‘wp_head’, ‘wlwmanifest_link’);

remove_action(‘wp_head’, ‘feed_links_extra’, 3);

remove_action(‘wp_head’, ‘adjacent_posts_rel_link’, 10, 0);

remove_action(‘wp_head’, ‘wp_shortlink_wp_head’, 10, 0 );

remove_action(‘wp_head’, ‘adjacent_posts_rel_link_wp_head’, 10, 0 );

remove_action(‘wp_head’, ‘wp_generator’);

function wpt_remove_version() {  

return ”;  

}  

add_filter(‘the_generator’, ‘wpt_remove_version’);

 

If your site is not used default posts or pages comments than add below function code in “General_function.php” and add_action, remove_action and add_filter Code in “Function.php”

// Disable support for comments and trackbacks in post types

function df_disable_comments_post_types_support() {

$post_types = get_post_types();

foreach ($post_types as $post_type) {

if(post_type_supports($post_type, ‘comments’)) {

remove_post_type_support($post_type, ‘comments’);

remove_post_type_support($post_type, ‘trackbacks’);

}

}

}

add_action(‘admin_init’, ‘df_disable_comments_post_types_support’);

 

// Close comments on the front-end

function df_disable_comments_status() {

return false;

}

add_filter(‘comments_open’, ‘df_disable_comments_status’, 20, 2);

add_filter(‘pings_open’, ‘df_disable_comments_status’, 20, 2);

// Hide existing comments

function df_disable_comments_hide_existing_comments($comments) {

$comments = array();

return $comments;

}

add_filter(‘comments_array’, ‘df_disable_comments_hide_existing_comments’, 10, 2);
// Remove comments page in menu

function df_disable_comments_admin_menu() {

remove_menu_page(‘edit-comments.php’);

}

add_action(‘admin_menu’, ‘df_disable_comments_admin_menu’);

 

// Redirect any user trying to access comments page

function df_disable_comments_admin_menu_redirect() {

global $pagenow;

if ($pagenow === ‘edit-comments.php’) {

wp_redirect(admin_url()); exit;

}

}

add_action(‘admin_init’, ‘df_disable_comments_admin_menu_redirect’);

 

// Remove comments metabox from dashboard

function df_disable_comments_dashboard() {

remove_meta_box(‘dashboard_recent_comments’, ‘dashboard’, ‘normal’);

}

add_action(‘admin_init’, ‘df_disable_comments_dashboard’);

 

// Remove comments links from admin bar

function df_disable_comments_admin_bar() {

if (is_admin_bar_showing()) {

remove_action(‘admin_bar_menu’, ‘wp_admin_bar_comments_menu’, 60);

}

}

add_action(‘init’, ‘df_disable_comments_admin_bar’);

 

If your site is not provide any search related functionality than add below code to your theme.

 

function fb_filter_query( $query, $error = true ) {

if ( is_search() ) {

$query->is_search = false;

$query->query_vars[s] = false;

$query->query[s] = false;

// to error

if ( $error == true ) {

$query->is_404 = true;

}

    }

}

 

add_action( ‘parse_query’, ‘fb_filter_query’ );

add_filter( ‘get_search_form’, create_function( ‘$a’, “return null;” ) );

 

Never do any changes in core files.

Always keep wordpress and plugins up to date with new version.

Change wordpress database prefix suitable to your theme name

Create files for functions in include folder :

include/themesetup.php for Theme setup and widgets

include/enqueue_scripts.php for Enqueue Scripts and style (Always include CSS and JS files here), if need some specific js or css file in particular template or post type then use condition here. Not include file in page.  

include/general_function.php for General Hooks , action and function used in theme globally 

include/custom_image_size.php for custom image sizes

 

If one functionality is used in more pages than create common functions or widget for this. 

When you want to add any inline “JS” for any variable declaration, then you have to add it using below way:

EX: 

  1. Add below Code in “include/enqueue_scripts.php” for set “AdminURL” variable.

function your_js_vars() { 

$admin_URL = ‘Your Admin URL’; // Your File Path

return array(

‘admin_URL’ =>  $admin_URL

);

}

wp_enqueue_script(‘your_script_name’, get_template_directory_uri( ) .’/js/your_js_name.js’, array(‘jquery’), ‘1.0.0’);

wp_localize_script( ‘your_script_name’, ‘your_unique_name’, your_js_vars());

 

  1. In “your_js_name.js” File you can use this variable as below way:

var my_custom_JS_Code = your_unique_name.admin_URL;

 

JavaScript & jQuery task is developer responsibility. It’s good our designer is create most of code in jQuery. But it’s developer responsibilities to check file and code.
Below Function only call should once.
JQuery(document).ready(function ()
jQuery(window).resize(function () {
jQuery(window).load(function () {

 

If you use click event of any button or object then it should not be in ready.

jQuery(document).on(‘click’, ‘.close-menu’, function (e) {

});

Remove alert and other testing code from js once complete your work.

For Contact form jquery changes use below valid method.
document.addEventListener(‘wpcf7mailsent’, function (event) {
if ( ‘1’ == event.detail.contactFormId ) {
}
}
document.addEventListener(‘wpcf7submit’, function (event) {
}

 

Never user fixed path for any images, css and js

Always put CSS, JS and Images in proper directory. Only style.css is put on theme directory root. And never write “JS” and “CSS” inline. If you have to need define any “JS” variable dynamically than always use “wp_localize_script” function.

Always use PHP comments. Never use HTML comments. HTML comments display on source.

After integration of static theme by designer you have to first make header, footer and sidebar dynamically.

When you complete with header, footer and sidebar dynamic portion than start work on page development.

Remove extra codes (variable, functions, comments etc…) from all files where you have to change functionality. If you want to backup code then use in your local system.

Never commit backup files.

Take Pull before push in directory. Resolve conflict before before push your code. If you can’t understand how to resolve this take help of your seniors and co-workers.

Use images with proper dimension. Don’t use full size images to everywhere. When you want to show thumbnail than you have to use “thumb image” code. In most of cases all of you have used large images code. Add custom image size in as per required.
add_image_size(‘image_name_slug’, Width, Height, array(position, position));
add_image_size(‘home_banner_image’, 1920, 786, array(canter, canter));

When you create any template than set prefix “tp-“

Ex: If you create template for “contact” page than your file name “tp-contact.php“.

When create custom post type than create file with “type-” prefix in ‘post-types’ folder

Ex: If you create custom post type “property” than your file name “type-property.php” in “post_type” Folder and include in “functions.php
Create particular post type and related function in this file.

If in your project there is no detail page for custom post type than hide it from front-end.

Ex: testimonials 

Always use proper naming for variable declaration.

String Variable : $strVarName

Array Variable : $arrVarName

Always use proper naming for all types of declaration (like variable, functions, templates etc.)

Always check if condition before below cases

Foreach()

Implode()

Get image url from Image_array

 

Always remove Archive and Draft link from ACF Page Link, Relational ship Fields. For this use one single function for different fields.
Ex.
add_filter(‘acf/fields/page_link/query/name=read_more_link’, ‘remove_draft_archive_link’, 10, 3);
add_filter(‘acf/fields/relationship/query/name=read_more_link’, ‘remove_draft_archive_link’, 10, 3);

 

Always use wordpress function with “get_” prefix not “the_”prefix.

Ex: 

the_title()” replace with “get_the_title()

the_permalink()” replace with “get_permalink()

 

When you create any template than always set suitable name. 

Common Mistakes: 

– When someone create new template using copy and paste than he/she always forgot to change “Template Name”

 

Save ACF Field Value in Variable before print direct in code and if ACF field not compulsory then check blank condition before print.
Wrong :
if(!empty(get_field(‘subtitle_text’))):
echo get_field(‘subtitle_text’);
endif;
Right :
$subtitle_text = get_field(‘subtitle_text’);
If(!empty($subtitle_text)):
echo $subtitle_text;
endif;

 

Add instruction comments for all functions which are created by you.

All external links always open in new window. So, you have to set “ target=’_blank’ ” for all external links. Set using function so external link without http:// or https:// will open in new tab.

When you do any projects than always create dummy records. So, you can do exact functionality for pagination, listing etc. You can use duplicate post plugin for this. If you have not having any content for site then use ‘Lorem ipsum’ text insted of ‘aaaaabb’, ‘asdfghijkl’.

When you have any anchor tag without any links than always add “javascript:void(0);

When you put any email address or phone number than set link for them.

For email use – mailto:admin@admin.com 

For Call use – callto:1234567

Single line html tag close as below. So, You never get W3Cvalidation error.

Ex

echo ‘<img src=”‘.$image[‘url’].'” class=”img-border” alt=”‘.$image[‘title’].'”/>’;

    instead of

echo ‘<img src=”‘.$image[‘url’].'” class=”img-border” alt=”‘.$image[‘title’].'”>’;

Never add any files for ajax call. Always call ajax using default wordpress functions (Use admin-ajax.php)
Send your data to ‘admin_url( ‘admin-ajax.php’ );’ and action parameter will be function name.
add_action(‘wp_ajax_load_portfolio’,’load_portfolio’);
add_action(‘wp_ajax_nopriv_load_portfolio’,’load_portfolio’);
function load_portfolio(){
}

 

When you have completed any pages than test it from your side completely as per client requirement than do post in group chat.

Always use Fly Dynamic Image Resizer plugin so we can reduce number of image file size

 

Plugins which we need to add when site live

Admin url change

Wordfence

Autoptimize

wp-fastest cache

Contact form DB if contact form 7 used

Yoast SEO

Captcha for Login

Stop user enumeration

Block bad queries

PHPCODE © 2024