Hello
Monday, May 29, 2023
How to backup and download Database using PHP
Thursday, May 25, 2023
Search functionality in wordpress custom theme
Hello. Today I saw you how to make search functionality in custom theme of wordpress.
Put this search form in your search area.
<form role="search" method="get" id="searchform" action="<?php echo esc_url( home_url( '/' ) ); ?>" class="sidebar__search-form">
<div>
<input type="text" value="<?php echo get_search_query(); ?>" name="s" id="s" placeholder="Keywrord.." />
<input type="submit" id="searchsubmit" value="Search" />
</div>
</form>
</div>
search.php
/**
* The template for displaying search results pages
*
* @link https://developer.wordpress.org/themes/basics/template-hierarchy/#search-result
*
* @package WordPress
* @subpackage Twenty_Seventeen
* @since 1.0
* @version 1.0
*/
get_header(); ?>
<section class="blog-details blog_single">
<div class="container">
<div class="row">
<div class="col-xl-8 col-lg-7 searchpage">
<p>You searched for : <?php printf( __( '%s', 'immigration' ), '<span>' . get_search_query() . '</span>' ); ?></p>
<?php
global $query_string;
$query_args = explode("&", $query_string);
$search_query = array();
foreach($query_args as $key => $string) {
$query_split = explode("=", $string);
$search_query[$query_split[0]] = urldecode($query_split[1]);
} // foreach
$the_query = new WP_Query($search_query);
if ( $the_query->have_posts() ) {
?>
<!-- the loop -->
<ol>
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<li>
<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
</li>
<?php endwhile; ?>
</ol>
<!-- end of the loop -->
<?php wp_reset_postdata(); ?>
<?php } else { ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php }
?>
</div>
<div class="col-xl-4 col-lg-5 blog-sidebar__col">
<div class="blog-sidebar__right">
<?php include ("sidebar-blog.php"); ?>
</div>
</div>
</div>
</div>
</section>
<?php get_footer(); ?>
Friday, May 5, 2023
Create custom post type and get it to frontend with code
Hello All
Today I show you how to create custom post type in functions.php and save those data and get it to frontend side.
function consultatns_custom() {
// Set UI labels for Custom Post Type
$labels = array(
'name' => _x( 'Consultant', 'Post Type General Name', 'immigration' ),
'singular_name' => _x( 'consultant', 'Post Type Singular Name', 'immigration' ),
'menu_name' => __( 'Consultant', 'immigration' ),
'parent_item_colon' => __( 'Parent Consultant', 'immigration' ),
'all_items' => __( 'All Consultants', 'immigration' ),
'view_item' => __( 'View consultant', 'immigration' ),
'add_new_item' => __( 'Add New consultant', 'immigration' ),
'add_new' => __( 'Add New', 'immigration' ),
'edit_item' => __( 'Edit consultant', 'immigration' ),
'update_item' => __( 'Update consultant', 'immigration' ),
'search_items' => __( 'Search consultant', 'immigration' ),
'not_found' => __( 'Not Found', 'immigration' ),
'not_found_in_trash' => __( 'Not found in Trash', 'immigration' ),
);
// Set other options for Custom Post Type
$args = array(
'label' => __( 'consultant', 'immigration' ),
'description' => __( 'All Consultant data', 'immigration' ),
'labels' => $labels,
// Features this CPT supports in Post Editor
'supports' => array( 'title', 'editor', 'excerpt', 'author', 'thumbnail', 'custom-fields' ),
// You can associate this CPT with a taxonomy or custom taxonomy.
//'taxonomies' => array( 'genres' ),
/* A hierarchical CPT is like Pages and can have
* Parent and child items. A non-hierarchical CPT
* is like Posts.
*/
'hierarchical' => false,
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'show_in_nav_menus' => true,
'show_in_admin_bar' => true,
'menu_position' => 5,
'can_export' => true,
'has_archive' => true,
'exclude_from_search' => false,
'publicly_queryable' => true,
'capability_type' => 'post',
'show_in_rest' => true,
'taxonomies' => array( 'category' ),
);
// Registering your Custom Post Type
register_post_type( 'consultant', $args );
}
/* Hook into the 'init' action so that the function
* Containing our post type registration is not
* unnecessarily executed.
*/
add_action( 'init', 'consultatns_custom', 5 );
//new metabox for consultant type
function wporg_add_custom_box() {
$screens = [ 'consultant', 'wporg_cpt' ];
foreach ( $screens as $screen ) {
add_meta_box(
'wporg_box_id', // Unique ID
'Social Media Links Here', // Box title
'wporg_custom_box_html', // Content callback, must be of type callable
$screen // Post type
);
}
}
add_action( 'add_meta_boxes', 'wporg_add_custom_box' );
function wporg_custom_box_html( $post ) {
$fblink = get_post_meta( $post->ID, 'metakey_facebook', true );
$instalink = get_post_meta( $post->ID, 'metakey_insta', true );
$twitterlink = get_post_meta( $post->ID, 'metakey_twitter', true );
$pinrestlink = get_post_meta( $post->ID, 'metakey_pinrest', true );
?>
<p> <label for="facebooklink">Facebook</label>
<input type="text" name="facebooklink" id="facebooklink" value="<?php echo $fblink; ?>"></p>
<p><label for="instalink">Instagram</label>
<input type="text" name="instalink" id="instalink" value="<?php echo $instalink; ?>"></p>
<p> <label for="twitterlink">Twitter</label>
<input type="text" name="twitterlink" id="twitterlink" value="<?php echo $twitterlink; ?>"></p>
<p><label for="pinrestlink">Pinrest</label>
<input type="text" name="pinrestlink" id="pinrestlink" value="<?php echo $pinrestlink; ?>"></p>
<?php
}
function wporg_save_postdata( $post_id ) {
if ( array_key_exists( 'facebooklink', $_POST ) ) {
update_post_meta(
$post_id,
'metakey_facebook',
$_POST['facebooklink']
);
}
if ( array_key_exists( 'instalink', $_POST ) ) {
update_post_meta(
$post_id,
'metakey_insta',
$_POST['instalink']
);
}
if ( array_key_exists( 'twitterlink', $_POST ) ) {
update_post_meta(
$post_id,
'metakey_twitter',
$_POST['twitterlink']
);
}
if ( array_key_exists( 'pinrestlink', $_POST ) ) {
update_post_meta(
$post_id,
'metakey_pinrest',
$_POST['pinrestlink']
);
}
}
add_action( 'save_post', 'wporg_save_postdata' );
//end consultant code
Create theme option with acf in wordpress theme
Hello All.
Today I want to show you how to create theme option pages with acf. Its very easy to create and use it. It saves your time.
Open your active theme's functions.php file and put this code:
if( function_exists('acf_add_options_page') ) {
acf_add_options_page(array(
'page_title' => 'Theme General Settings',
'menu_title' => 'Theme Settings',
'menu_slug' => 'theme-general-settings',
'capability' => 'edit_posts',
'redirect' => false
));
acf_add_options_sub_page(array(
'page_title' => 'Theme Header Settings',
'menu_title' => 'Header',
'parent_slug' => 'theme-general-settings',
));
acf_add_options_sub_page(array(
'page_title' => 'Theme Footer Settings',
'menu_title' => 'Footer',
'parent_slug' => 'theme-general-settings',
));
acf_add_options_sub_page(array(
'page_title' => 'Single Page Sidebar Settings',
'menu_title' => 'Single Page Sidebar',
'parent_slug' => 'theme-general-settings',
));
}
//end option page
It will create like this look in your wordpress
Now go to Custom FIelds -> Field Groups
Create new field group and set it location by set rules. Means where to display this page.
Go to Location section, click on first field and select "Option Page"
Select "is equal to"
Select "Theme Header Setting"
Here I set theme header. I want to set header data like logo, contact number, and other header details.
If you want to use it in your theme, you can use it anywhere. But you can not use in header.php and footer.php file directly. You have to include it first like this.
<?php get_header(); ?>
In header.php file, put this above line in top. After this code, you can start html code but this get_header() is must put.
If you want to use custom option page data in anywhere, you can use like this:
if ( get_field('logo','option') ) {
$logo = get_field('logo','option');
} else {
$logo = get_template_directory_uri().'/assets/logo-1.png';
}
?>
<?php if ( get_field('updates_text','option') ) { ?>
<p><?php the_field('updates_text','option') ?></p>
<?php } ?>
Inshort "option" is must include in get_field or the_field function.
If you want to use any acf fields in particular template, use like this:-
<h3><?php the_field('treck_offices'); ?></h3>
<?php } ?>
For repeater field use this code:-
<ul class="coaching-details__benefit-points list-unstyled">
<?php while(have_rows('benifit_list')) : the_row(); ?>
<li>
<div class="icon">
span class="icon-check"></span>
</div>
<div class="text">
<p><?php the_sub_field('coaching_benifit_list'); ?></p>
</div>
</li>
<?php endwhile; ?>
</ul>
<?php } ?>
Wednesday, May 3, 2023
Modify comment section and create new in wordpress
Hello
Here I explain how to modify comment section.
it look like this :
And code is here.
functions.php
wp_enqueue_script( 'comment-reply' );
}
add_filter( 'comment_form_fields', 'custom_comment_field' );
function custom_comment_field( $fields ) {
// What fields you want to control.
$comment_field = $fields['author'];
$comment_field = $fields['email'];
$comment_field = $fields['comment'];
$comment_field = $fields['cookies'];
// The fields you want to unset (remove).
unset($fields['author']);
unset($fields['email']);
unset($fields['url']);
unset($fields['comment']);
unset($fields['cookies']);
// Display the fields to your own taste.
// The order in which you place them will determine in what order they are displayed.
$fields['author'] = '<div class="row"><div class="col-xl-6"><div class="comment-form__input-box"><input type="text" id="author" name="author" require="required" placeholder="Your Name" ></div></div> ';
$fields['email'] = '<div class="col-xl-6"><div class="comment-form__input-box"><input type="text" id="email" name="email" require="required" placeholder="Your Email"></div></div></div>';
$fields['comment'] = '<div class="row">
<div class="col-xl-12"><div class="comment-form__input-box text-message-box"><textarea id="comment" name="comment" required="required" placeholder="Your Comment"></textarea></div></div></div>';
$fields['cookies'] = '<p class="comment-form-cookies-consent"><input id="wp-comment-cookies-consent" name="wp-comment-cookies-consent" type="checkbox" value="yes"><label for="wp-comment-cookies-consent"> Save details for future comments?</label></p>';
return $fields;
}
Create custom post type with some extra custom fields and display it to frontend side without plugin in wordpress
Hello
Today I want to explain How to create custom post type and its custom fields and displayed it to frontend without any plugin. You have to do it in your theme's functions.php file.
Above is from backend. Now in frontend, it look like this:-
Now its code
functions.php
function consultatns_custom() {
// Set UI labels for Custom Post Type
$labels = array(
'name' => _x( 'Consultant', 'Post Type General Name', 'immigration' ),
'singular_name' => _x( 'consultant', 'Post Type Singular Name', 'immigration' ),
'menu_name' => __( 'Consultant', 'immigration' ),
'parent_item_colon' => __( 'Parent Consultant', 'immigration' ),
'all_items' => __( 'All Consultants', 'immigration' ),
'view_item' => __( 'View consultant', 'immigration' ),
'add_new_item' => __( 'Add New consultant', 'immigration' ),
'add_new' => __( 'Add New', 'immigration' ),
'edit_item' => __( 'Edit consultant', 'immigration' ),
'update_item' => __( 'Update consultant', 'immigration' ),
'search_items' => __( 'Search consultant', 'immigration' ),
'not_found' => __( 'Not Found', 'immigration' ),
'not_found_in_trash' => __( 'Not found in Trash', 'immigration' ),
);
// Set other options for Custom Post Type
$args = array(
'label' => __( 'consultant', 'immigration' ),
'description' => __( 'All Consultant data', 'immigration' ),
'labels' => $labels,
// Features this CPT supports in Post Editor
'supports' => array( 'title', 'editor', 'excerpt', 'author', 'thumbnail', 'custom-fields' ),
// You can associate this CPT with a taxonomy or custom taxonomy.
//'taxonomies' => array( 'genres' ),
/* A hierarchical CPT is like Pages and can have
* Parent and child items. A non-hierarchical CPT
* is like Posts.
*/
'hierarchical' => false,
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'show_in_nav_menus' => true,
'show_in_admin_bar' => true,
'menu_position' => 5,
'can_export' => true,
'has_archive' => true,
'exclude_from_search' => false,
'publicly_queryable' => true,
'capability_type' => 'post',
'show_in_rest' => true,
'taxonomies' => array( 'category' ),
);
// Registering your Custom Post Type
register_post_type( 'consultant', $args );
}
/* Hook into the 'init' action so that the function
* Containing our post type registration is not
* unnecessarily executed.
*/
add_action( 'init', 'consultatns_custom', 5 );
//code for custom fields or meta box
//new metabox for consultant type
function wporg_add_custom_box() {
$screens = [ 'consultant', 'wporg_cpt' ];
foreach ( $screens as $screen ) {
add_meta_box(
'wporg_box_id', // Unique ID
'Social Media Links Here', // Box title
'wporg_custom_box_html', // Content callback, must be of type callable
$screen // Post type
);
}
}
add_action( 'add_meta_boxes', 'wporg_add_custom_box' );
function wporg_custom_box_html( $post ) {
$fblink = get_post_meta( $post->ID, 'metakey_facebook', true );
$instalink = get_post_meta( $post->ID, 'metakey_insta', true );
$twitterlink = get_post_meta( $post->ID, 'metakey_twitter', true );
$pinrestlink = get_post_meta( $post->ID, 'metakey_pinrest', true );
?>
<p> <label for="facebooklink">Facebook</label>
<input type="text" name="facebooklink" id="facebooklink" value="<?php echo $fblink; ?>"></p>
<p><label for="instalink">Instagram</label>
<input type="text" name="instalink" id="instalink" value="<?php echo $instalink; ?>"></p>
<p> <label for="twitterlink">Twitter</label>
<input type="text" name="twitterlink" id="twitterlink" value="<?php echo $twitterlink; ?>"></p>
<p><label for="pinrestlink">Pinrest</label>
<input type="text" name="pinrestlink" id="pinrestlink" value="<?php echo $pinrestlink; ?>"></p>
<?php
}
function wporg_save_postdata( $post_id ) {
if ( array_key_exists( 'facebooklink', $_POST ) ) {
update_post_meta(
$post_id,
'metakey_facebook',
$_POST['facebooklink']
);
}
if ( array_key_exists( 'instalink', $_POST ) ) {
update_post_meta(
$post_id,
'metakey_insta',
$_POST['instalink']
);
}
if ( array_key_exists( 'twitterlink', $_POST ) ) {
update_post_meta(
$post_id,
'metakey_twitter',
$_POST['twitterlink']
);
}
if ( array_key_exists( 'pinrestlink', $_POST ) ) {
update_post_meta(
$post_id,
'metakey_pinrest',
$_POST['pinrestlink']
);
}
}
add_action( 'save_post', 'wporg_save_postdata' );
//end consultant code
Frontend Code:-
frontend.php
$args = array(
'post_type' => 'consultant',
'posts_per_page' => 4,
'post_status' => 'publish',
'orderby' => 'date',
'order' => 'DESC',
);
$query = new WP_Query( $args );
if ( $query->have_posts() ) {
echo '<div class="row">';
$secondsdelay = 0;
while ( $query->have_posts() ) {
$secondsdelay = $secondsdelay + 100;
$secondsset = $secondsdelay.'ms';
$data = $query->the_post();
$post_thumbnail_id = get_post_thumbnail_id( $data );
$image = wp_get_attachment_image_src( get_post_thumbnail_id( get_the_ID() ), 'single-post-thumbnail' );
$title = get_the_title();
$permalink = get_the_permalink();
$excerpt = get_the_excerpt();
$content = get_the_content();
//get custom fields data
//$custom = get_post_custom( get_the_ID() );
$fblink = get_post_meta(get_the_ID(),'metakey_facebook',true);
$instalink = get_post_meta(get_the_ID(),'metakey_insta',true);
$twitterlink = get_post_meta(get_the_ID(),'metakey_twitter',true);
$pinrestlink = get_post_meta(get_the_ID(),'metakey_pinrest',true);
?>
<!--Team One Single Start-->
<div class="col-xl-3 col-lg-6 col-md-6 wow fadeInUp animated" data-wow-delay="<?php echo $secondsset; ?>" style="visibility: visible; animation-delay: <?php echo $secondsset; ?>; animation-name: fadeInUp;">
<div class="team-one__single">
<div class="team-one__img-box">
<div class="team-one__img">
<img src="<?php echo $image[0]; ?>" alt="">
</div>
<div class="team-one__share-btn">
<a href="<?php echo $permalink; ?>"><i class="fa fa-share-alt"></i></a>
</div>
<ul class="list-unstyled team-one__social">
<li><a href="<?php echo $fblink; ?>"><i class="fab fa-facebook"></i></a></li>
<li><a href="<?php echo $instalink; ?>"><i class="fab fa-twitter"></i></a></li>
<li><a href="<?php echo $twitterlink; ?>"><i class="fab fa-pinterest-p"></i></a></li>
<li><a href="<?php echo $pinrestlink; ?>"><i class="fab fa-instagram"></i></a></li>
</ul>
</div>
<div class="team-one__content">
<p class="team-one__sub-title">Consultants</p>
<h3 class="team-one__title"><a href="<?php echo $permalink; ?>"><?php echo $title; ?></a></h3>
<div class="team-one__arrow-box">
<a href="<?php echo $permalink; ?>" class="team-one__arrow"><i class="fa fa-angle-right"></i></a>
</div>
</div>
</div>
</div>
<!--Team One Single End-->
<?php }
echo '</div>';
}
// Reset Post Data
wp_reset_postdata();
?>