It returns a string of the post type of the post ID or the current post, from this value you can check on the post type to decide what you do with it, just like the code. Add the below code to the wp-includes/post.php
It determines whether the query is for an existing post under any page type (Blog post, attachment, page, custom post types). It enables you to target single-view pages, regular page pages, and the attachment page all in one swoop. Add this below code to wp-includes/query.php
function is_singular( $post_types = '' ) {
global $wp_query;
if ( ! isset( $wp_query ) ) {
_doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1.0' );
return false;
}
return $wp_query->is_singular( $post_types );
}
It includes a template part of the theme. Providing a simple mechanism for child themes to overload reusable sections of code in the theme. Add the below code in wp-includes/general-template.php.
It is used to clean and escape for output as a URL and to convert and fixe the HTML entities. Sanitizes a string and removes disallowed URL protocols. Retrieve a list of protocols to allow in HTML attributes. Add this below code in wp-includes/plugin.php
It is used to check whether you need to install an option. get_option() is commonly used during the installation of plugin options and to test whether upgrading is required or not. Add this below code in wp-includes/plugin.php
function get_option( $option, $default = false ) {
global $wpdb;
$option = trim( $option );
if ( empty( $option ) ) {
return false;
}
$pre = apply_filters( "pre_option_{$option}", false, $option, $default );
if ( false !== $pre ) {
return $pre;
}
if ( defined( 'WP_SETUP_CONFIG' ) ) {
return false;
}
$passed_default = func_num_args() > 1;
if ( ! wp_installing() ) {
// prevent non-existent options from triggering multiple queries
$notoptions = wp_cache_get( 'notoptions', 'options' );
if ( isset( $notoptions[ $option ] ) ) {
return apply_filters( "default_option_{$option}", $default, $option, $passed_default );
}
$alloptions = wp_load_alloptions();
if ( isset( $alloptions[ $option ] ) ) {
$value = $alloptions[ $option ];
} else {
$value = wp_cache_get( $option, 'options' );
if ( false === $value ) {
$row = $wpdb->get_row( $wpdb->prepare( "SELECT option_value FROM $wpdb->options WHERE option_name = %s LIMIT 1", $option ) );
// Has to be get_row instead of get_var because of funkiness with 0, false, null values
if ( is_object( $row ) ) {
$value = $row->option_value;
wp_cache_add( $option, $value, 'options' );
} else { // option does not exist, so we must cache its non-existence
if ( ! is_array( $notoptions ) ) {
$notoptions = array();
}
$notoptions[ $option ] = true;
wp_cache_set( 'notoptions', $notoptions, 'options' );
/** This filter is documented in wp-includes/option.php */
return apply_filters( "default_option_{$option}", $default, $option, $passed_default );
}
}
}
} else {
$suppress = $wpdb->suppress_errors();
$row = $wpdb->get_row( $wpdb->prepare( "SELECT option_value FROM $wpdb->options WHERE option_name = %s LIMIT 1", $option ) );
$wpdb->suppress_errors( $suppress );
if ( is_object( $row ) ) {
$value = $row->option_value;
} else {
/** This filter is documented in wp-includes/option.php */
return apply_filters( "default_option_{$option}", $default, $option, $passed_default );
}
}
if ( 'home' == $option && '' == $value ) {
return get_option( 'siteurl' );
}
if ( in_array( $option, array( 'siteurl', 'home', 'category_base', 'tag_base' ) ) ) {
$value = untrailingslashit( $value );
}
return apply_filters( "option_{$option}", maybe_unserialize( $value ), $option );
}
This is a callback function that has to been added to a filter hook. The callback functions attached to the filter hook are invoked by calling this function. apply_filters() function can be used to create a new filter hook by simply calling this function with the name of the new hook specified using the $tag parameter. Add this below code in wp-includes/plugin.php.
get_theme_mod() is used to filter the theme modification, retrieve all the theme modifications. Add this below code in wp-includes/theme.php to do the same.
<?php Function get_theme_mod($name, $default = false) {
$mod = get_theme_mods();
if( isset( $mod [ $name ] ) ) {
Return apply_filters( "theme_mod_{$name}", $mod[ $name ]);
}
if ( is_string( $default ) ) {
// Only run the replacement if an sprintf() string format pattern was found.
if ( preg_match( '#(?<!%)%(?:\d+\$?)?s#', $default ) ) {
$default = sprintf( $default, get_template_directory_uri(), get_stylesheet_directory_uri() );
}
}
return apply_filters( "theme_mod_{$name}", $default );
}
Are you looking for a way to allow the admin to band WordPress user accounts? While there’s probably a plugin for this, we have created a quick code snippet that you can use to ban users accounts in WordPress.
All you have to do is add this code to your theme’s functions.php file or in a site-specific plugin:
// display checkbox to admin
add_action( 'edit_user_profile', 'ban_user_profile_fields' );
function ban_user_profile_fields( $user ) {
global $current_user;
if ( current_user_can( 'edit_user' ) && $user->ID != $current_user->ID ){
$status = get_the_author_meta( 'ban_user', $user->ID );
?>
<h3><?php _e("Account Status", "blank"); ?></h3>
<table class="form-table">
<tr>
<th>Ban User</th>
<td><label for="ban_user"><input type="checkbox" name="ban_user" id="ban_user" value="1" <?php if($status == 1){ echo ' checked'; } ?> /></label>
<span class="description"><?php _e("Check this option to ban this users account."); ?></span>
</td>
</tr>
</table>
<?php
}
}
// Save profile update
add_action( 'edit_user_profile_update', 'save_extra_user_profile_fields' );
function save_extra_user_profile_fields( $user_id ){
if ( !current_user_can( 'edit_user', $user_id ) ) { return false; }
update_usermeta( $user_id, 'ban_user', $_POST['ban_user'] );
}
// Check if user is banned
add_filter( 'wp_authenticate_user', 'login_ban_status', 1 );
function login_ban_status($user) {
if ( is_wp_error( $user ) ) { return $user; }
$status = get_user_meta( $user->ID, 'ban_user', 'true' );
if($status == 1){
return new WP_Error( 'banned', __('<strong>ERROR</strong>: This user account has been banned.', 'banned') );
}
return $user;
}
Note: If this is your first time adding code snippets in WordPress, then please refer to our guide on how to properly copy / paste code snippets in WordPress, so you don’t accidentally break your site.