Enter your keyword

Blogs

How to Validate Numbers in Contact Form 7

function is_number( $result, $tag ) {
$type = $tag['type'];
$name = $tag['name'];

if ($name == 'phone' || $name == 'fax') { // Validation applies to these textfield names. Add more with || inbetween
$stripped = preg_replace( '/\D/', '', $_POST[$name] );
$_POST[$name] = $stripped;
if( strlen( $_POST[$name] ) != 10 ) { // Number string must equal this
$result['valid'] = false;
$result['reason'][$name] = $_POST[$name] = 'Please enter a 10 digit phone number.';
}
}
return $result;
}

add_filter( 'wpcf7_validate_text', 'is_number', 10, 2 );
add_filter( 'wpcf7_validate_text*', 'is_number', 10, 2 );

How to Change Wordpress admin Texts

add_filter('gettext',  'translate_text1');
add_filter('ngettext',  'translate_text1');
function translate_text1($translated) {
    $translated = str_ireplace('Wordpress',  'DS Web Solutions',  $translated);
    return $translated;
}

How to Change Wordpress Site Location

Paste the query in database SQL server.

1.  Update wp_options table.

UPDATE wp_options SET option_value = replace(option_value, 'http://www.oldsite.com', 'http://www.newsite.com') WHERE option_name = 'home' OR option_name = 'siteurl';

2.  Update wp_posts table set guid.

UPDATE wp_posts SET guid = replace(guid, 'http://www.oldsite.com', 'http://www.newsite.com');

3.  Update wp_posts table set post_content.

UPDATE wp_posts SET post_content = replace(post_content, 'http://www.oldsite.com', 'http://www.newsite.com');

How to Compression of js and css in using .htaccess

Add this code to your .htaccess file

RewriteEngine on
RewriteCond %{HTTP:Accept-Encoding} gzip
RewriteCond %{REQUEST_FILENAME}.jgz -f
RewriteRule (.*).js$ $1.js.jgz [L]
AddType "text/javascript" .js.jgz
AddEncoding gzip .jgz

RewriteCond %{HTTP:Accept-Encoding} gzip
RewriteCond %{REQUEST_FILENAME}.jgz -f
RewriteRule (.*).css$ $1.css.jgz [L]
AddType "text/css" .css.jgz
AddEncoding gzip .jgz
Options -Indexes

How to Get Custom Menu Items

add_filter('wp_nav_menu_items', 'wp_nav_menu_custom', 10, 2);
function wp_nav_menu_custom($items, $args) {
if( $args->theme_location == 'primary' ) {

$menu_name = 'primary';
$locations = get_nav_menu_locations();
//echo '<pre/>'; print_r($locations); die;
$menu = wp_get_nav_menu_object(4, $menu_name );
$menuitems = wp_get_nav_menu_items( $menu->term_id , array( 'order' => 'DESC' ) );

foreach ( $menuitems as $item ){

$id = get_post_meta( $item->ID,'_menu_item_object_id', true );
echo '<br/>';
echo $id;
//$page = get_page( $id );
//$link = get_page_link( $id );
}

}
}

How to Add Custom Fields in WordPress Comment Form

<?php 
// Add custom meta (ratings) fields to the default comment form

// Default comment form includes name, email and URL
// Default comment form elements are hidden when user is logged in

add_filter('comment_form_default_fields','custom_fields');
function custom_fields($fields) {

$commenter = wp_get_current_commenter();
$req = get_option( 'require_name_email' );
$aria_req = ( $req ? " aria-required='true'" : '' );

$fields[ 'author' ] = '<p class="comment-form-author">'.
'<label for="author">' . __( 'Name' ) . '</label>'.
( $req ? '<span class="required">*</span>' : '' ).
'<input id="author" name="author" type="text" value="'. esc_attr( $commenter['comment_author'] ) .
'" size="30″ tabindex="1″' . $aria_req . ' /></p>';

$fields[ 'email' ] = '<p class="comment-form-email">'.
'<label for="email">' . __( 'Email' ) . '</label>'.
( $req ? .'<span class="required">*</span>' : '' ).
'<input id="email" name="email" type="text" value="'. esc_attr( $commenter['comment_author_email'] ) .
'" size="30″ tabindex="2″' . $aria_req . ' /></p>';

$fields[ 'url' ] = '<p class="comment-form-url">'.
'<label for="url">' . __( 'Website' ) . '</label>'.
'<input id="url" name="url" type="text" value="'. esc_attr( $commenter['comment_author_url'] ) .
'" size="30″ tabindex="3″ /></p>';

$fields[ 'phone' ] = '<p class="comment-form-phone">'.
'<label for="phone">' . __( 'Phone' ) . '</label>'.
'<input id="phone" name="phone" type="text" size="30″ tabindex="4″ /></p>';

return $fields;
}

// Add fields after default fields above the comment box, always visible

add_action( 'comment_form_logged_in_after', 'additional_fields' );
add_action( 'comment_form_after_fields', 'additional_fields' );

function additional_fields () {
echo '<p class="comment-form-title">'.
'<label for="title">' . __( 'Comment Title' ) . '</label>'.
'<input id="title" name="title" type="text" size="30″ tabindex="5″ /></p>';

echo '<p class="comment-form-rating">'.
'<label for="rating">'. __('Rating') . '<span class="required">*</span></label>
<span class="commentratingbox">';

for( $i=1; $i <= 5; $i++ )
echo '<span class="commentrating"><input type="radio" name="rating" id="rating" value="'. $i .'"/>'. $i .'</span>';

echo'</span></p>';

}

// Save the comment meta data along with comment

add_action( 'comment_post', 'save_comment_meta_data' );
function save_comment_meta_data( $comment_id ) {
if ( ( isset( $_POST['phone'] ) ) && ( $_POST['phone'] != '') )
$phone = wp_filter_nohtml_kses($_POST['phone']);
add_comment_meta( $comment_id, 'phone', $phone );

if ( ( isset( $_POST['title'] ) ) && ( $_POST['title'] != '') )
$title = wp_filter_nohtml_kses($_POST['title']);
add_comment_meta( $comment_id, 'title', $title );

if ( ( isset( $_POST['rating'] ) ) && ( $_POST['rating'] != '') )
$rating = wp_filter_nohtml_kses($_POST['rating']);
add_comment_meta( $comment_id, 'rating', $rating );
}
// Add the filter to check if the comment meta data has been filled or not

add_filter( 'preprocess_comment', 'verify_comment_meta_data' );
function verify_comment_meta_data( $commentdata ) {
if ( ! isset( $_POST['rating'] ) )
wp_die( __( 'Error: You did not add your rating. Hit the BACK button of your Web browser and resubmit your comment with rating.' ) );
return $commentdata;
}

//Add an edit option in comment edit screen

add_action( 'add_meta_boxes_comment', 'extend_comment_add_meta_box' );
function extend_comment_add_meta_box() {
add_meta_box( 'title', __( 'Comment Metadata – Extend Comment' ), 'extend_comment_meta_box', 'comment', 'normal', 'high' );
}

function extend_comment_meta_box ( $comment ) {
$phone = get_comment_meta( $comment->comment_ID, 'phone', true );
$title = get_comment_meta( $comment->comment_ID, 'title', true );
$rating = get_comment_meta( $comment->comment_ID, 'rating', true );
wp_nonce_field( 'extend_comment_update', 'extend_comment_update', false );
?>
<p>
<label for="phone"><?php _e( 'Phone' ); ?></label>
<input type="text" name="phone" value="<?php echo esc_attr( $phone ); ?>" class="widefat" />
</p>
<p>
<label for="title"><?php _e( 'Comment Title' ); ?></label>
<input type="text" name="title" value="<?php echo esc_attr( $title ); ?>" class="widefat" />
</p>
<p>
<label for="rating"><?php _e( 'Rating: ' ); ?></label>
<span class="commentratingbox">
<?php for( $i=1; $i <= 5; $i++ ) {
echo '<span class="commentrating"><input type="radio" name="rating" id="rating" value="'. $i .'"';
if ( $rating == $i ) echo ' checked="checked"';
echo ' />'. $i .' </span>';
}
?>
</span>
</p>
<?php
}

// Update comment meta data from comment edit screen

add_action( 'edit_comment', 'extend_comment_edit_metafields' );
function extend_comment_edit_metafields( $comment_id ) {
if( ! isset( $_POST['extend_comment_update'] ) || ! wp_verify_nonce( $_POST['extend_comment_update'], 'extend_comment_update' ) ) return;

if ( ( isset( $_POST['phone'] ) ) && ( $_POST['phone'] != '') ) :
$phone = wp_filter_nohtml_kses($_POST['phone']);
update_comment_meta( $comment_id, 'phone', $phone );
else :
delete_comment_meta( $comment_id, 'phone');
endif;

if ( ( isset( $_POST['title'] ) ) && ( $_POST['title'] != '') ):
$title = wp_filter_nohtml_kses($_POST['title']);
update_comment_meta( $comment_id, 'title', $title );
else :
delete_comment_meta( $comment_id, 'title');
endif;

if ( ( isset( $_POST['rating'] ) ) && ( $_POST['rating'] != '') ):
$rating = wp_filter_nohtml_kses($_POST['rating']);
update_comment_meta( $comment_id, 'rating', $rating );
else :
delete_comment_meta( $comment_id, 'rating');
endif;

}

// Add the comment meta (saved earlier) to the comment text
// You can also output the comment meta values directly in comments template

add_filter( 'comment_text', 'modify_comment');
function modify_comment( $text ){

$plugin_url_path = WP_PLUGIN_URL;

if( $commenttitle = get_comment_meta( get_comment_ID(), 'title', true ) ) {
$commenttitle = '<strong>' . esc_attr( $commenttitle ) . '</strong><br/>';
$text = $commenttitle . $text;
}

if( $commentrating = get_comment_meta( get_comment_ID(), 'rating', true ) ) {
$commentrating = '<p class="comment-rating"> <img src="'. $plugin_url_path .
'/ExtendComment/images/'. $commentrating . 'star.gif"/><br/>Rating: <strong>'. $commentrating .' / 5</strong></p>';
$text = $text . $commentrating;
return $text;
} else {
return $text;
}
}


?>

WP list Categories Pagination

<?php
    $args = array(
            'taxonomy' => 'categories',
            'orderby' => 'term_group',
            'hide_empty' => 0,
            'hierarchical' => 1,
            'exclude' => '16',
            'parent' => '0',
    );
    $categories = get_categories($args);
    $numOfItems = 60;
    $page = isset( $_GET['cpage'] ) ? abs( (int) $_GET['cpage'] ) : 1;
    $to = $page * $numOfItems;
    $current = $to - $numOfItems;
    $total = sizeof($categories);

            echo '<ul>';

    for ($i=$current; $i<$to; ++$i) {

        $category = $categories[$i];

        if ($category->name) { echo '<li><a href="' . get_term_link($category->slug, 'categories') . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' . $category->name.'</a></li>';}

    }

            echo '</ul>';

    unset($category);
    echo paginate_links( array(
        'base' => add_query_arg( 'cpage', '%#%' ),
        'format' => '',
        'prev_text' => __('&laquo;'),
        'next_text' => __('&raquo;'),
        'total' => ceil($total / $numOfItems),
        'current' => $page
    ));

    ?>

CURL Request

$url='https://maps.googleapis.com/maps/api/place/textsearch/json?query='.$query1.'&sensor=true&key='.$key;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_PROXYPORT, 3128);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
$response = curl_exec($ch);
curl_close($ch);
$response_a = json_decode($response);
echo '<pre/>'; print_r($response_a); die;

Update Term count by SQL

Update WordPress Taxonomies (Categories/Tags) count field after bulk Import/Delete

UPDATE wp_term_taxonomy SET count = (
SELECT COUNT(*) FROM wp_term_relationships rel
LEFT JOIN wp_posts po ON (po.ID = rel.object_id)
WHERE
rel.term_taxonomy_id = wp_term_taxonomy.term_taxonomy_id
AND
wp_term_taxonomy.taxonomy NOT IN ('link_category')
AND
po.post_status IN ('publish', 'future')
)

How to Create a New Custom Taxonomy in WordPress

function genres() {
    // Add new "Locations" taxonomy to Posts
    register_taxonomy('genres_cat', 'music_review', array(
        // Hierarchical taxonomy (like categories)
        'hierarchical' => true,
        // This array of options controls the labels displayed in the WordPress Admin UI
        'labels' => array(
            'name' => _x( 'Category', 'taxonomy general name' ),
            'singular_name' => _x( 'Category', 'taxonomy singular name' ),
            'search_items' =>  __( 'Search Category' ),
            'all_items' => __( 'All Category' ),
            'parent_item' => __( 'Parent Category' ),
            'parent_item_colon' => __( 'Parent Category:' ),
            'edit_item' => __( 'Edit Category' ),
            'update_item' => __( 'Update Category' ),
            'add_new_item' => __( 'Add New Category' ),
            'new_item_name' => __( 'New Category Name' ),
            'menu_name' => __( 'Category' ),
        ),
        // Control the slugs used for this taxonomy
        'rewrite' => array(
            'slug' => 'genres_cat', // This controls the base slug that will display before each term
            'with_front' => false, // Don't display the category base before "/locations/"
            'hierarchical' => true // This will allow URL's like "/locations/boston/cambridge/"
        ),
    ));
}
add_action( 'init', 'genres', 0 );

How to Create Custom Post Type in WordPress

function register_cpt_music_review() {
 
    $labels = array(
        'name' => _x( 'Music Reviews', 'music_review' ),
        'singular_name' => _x( 'Music Review', 'music_review' ),
        'add_new' => _x( 'Add New', 'music_review' ),
        'add_new_item' => _x( 'Add New Music Review', 'music_review' ),
        'edit_item' => _x( 'Edit Music Review', 'music_review' ),
        'new_item' => _x( 'New Music Review', 'music_review' ),
        'view_item' => _x( 'View Music Review', 'music_review' ),
        'search_items' => _x( 'Search Music Reviews', 'music_review' ),
        'not_found' => _x( 'No music reviews found', 'music_review' ),
        'not_found_in_trash' => _x( 'No music reviews found in Trash', 'music_review' ),
        'parent_item_colon' => _x( 'Parent Music Review:', 'music_review' ),
        'menu_name' => _x( 'Music Reviews', 'music_review' ),
    );
 
    $args = array(
        'labels' => $labels,
        'hierarchical' => true,
        'description' => 'Music reviews filterable by genre',
        'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'comments', 'revisions', 'page-attributes' ),
        'taxonomies' => array( 'genres' ),
        'public' => true,
        'show_ui' => true,
        'show_in_menu' => true,
        'menu_position' => 5,
        'menu_icon' => 'dashicons-format-audio',
        'show_in_nav_menus' => true,
        'publicly_queryable' => true,
        'exclude_from_search' => false,
        'has_archive' => true,
        'query_var' => true,
        'can_export' => true,
        'rewrite' => true,
        'capability_type' => 'post'
    );
 
    register_post_type( 'music_review', $args );
}
 
add_action( 'init', 'register_cpt_music_review' );

Add Checkbox to send mail copy to self in contact form7

Step1:

Add this function in theme functions.php

function check_mail_send_contactform($cf7) {
//get CF7's mail and posted_data objects
$submission = WPCF7_Submission::get_instance();
if ( $submission ) {
$posted_data = $submission->get_posted_data();
}
$mail = $cf7->prop( 'mail' );

if($posted_data['contact_sendmail'][0]) { //if Checkbox checked
$mail2 = $cf7->prop( 'mail_2′ ); //get CF7's mail_2 object
//now activate mail_2, the value 0 means not active
$mail2['active'] = 1;
$cf7->set_properties( array( 'mail_2′ => $mail2 ) );
}
return $cf7;
}
add_action('wpcf7_before_send_mail','check_mail_send_contactform');

 

Step2:

Add this Shortcode in Contact form:

/* This shortcode add checkbox in contact Form */
[checkbox contact_sendmail default:1 "Send a copy of this email to yourself"]

Step3:

UnCheck on Use mail (2) checkbox in contact form7 and put contact form mail field code into the To fields in Mail (2) Section.

How To Add Custom Stylesheet In Genesis Theme

add_action( 'wp_enqueue_scripts', 'custom_add_custom_stylesheet' );
function custom_add_custom_stylesheet() {
    wp_enqueue_style( 'custom-stylesheet', get_stylesheet_directory_uri() . '/custom.css', array(), PARENT_THEME_VERSION );
}

How to add custom Scripts into Genesis Child Theme

 

// Add add custom Scripts into Genesis Child Theme
add_action( 'wp_enqueue_scripts', 'dsweb_enqueue_script' );
function dsweb_enqueue_script() {
    wp_enqueue_script( 'customscript', get_stylesheet_directory_uri() . '/js/custom.js', array( 'jquery' ), '', true );
}

 

How to Show Featured Image in Rss Feed

Add This Code functions.php for Show Thumbnail Featured Image in Rss Feed

function featured_image_in_rss($content) {
global $post;
if ( has_post_thumbnail( $post->ID ) ){
$content = '' . get_the_post_thumbnail( $post->ID, 'thumbnail') . '
' . $content.'
'; } return $content; } add_filter('the_excerpt_rss', 'featured_image_in_rss'); add_filter('the_content_feed', 'featured_image_in_rss');

If you want display larger images instead, change the word 'thumbnail' to 'medium', 'large' or 'full'.

Add This Code functions.php for Show Full Size Featured Image in Rss Feed

function featured_image_in_rss($content) {
global $post;
if ( has_post_thumbnail( $post->ID ) ){
$content = '' . get_the_post_thumbnail( $post->ID, 'full') . '
' . $content.'
'; } return $content; } add_filter('the_excerpt_rss', 'featured_image_in_rss'); add_filter('the_content_feed', 'featured_image_in_rss');