<?php
/*
Template Name: Register Form
*/
get_header();
global $wpdb, $user_ID;
//Check whether the user is already logged in
if ($user_ID)
{
// They're already logged in, so we bounce them back to the homepage.
header( 'Location:' . home_url() );
} else
{
$errors = array();
if( $_SERVER['REQUEST_METHOD'] == 'POST' )
{
// Check username is present and not already in use
$username = $wpdb->escape($_REQUEST['username']);
if ( strpos($username, ' ') !== false )
{
$errors['username'] = "Sorry, no spaces allowed in usernames";
}
if(emptyempty($username))
{
$errors['username'] = "Please enter a username";
} elseif( username_exists( $username ) )
{
$errors['username'] = "Username already exists, please try another";
}
// Check email address is present and valid
$email = $wpdb->escape($_REQUEST['email']);
if( !is_email( $email ) )
{
$errors['email'] = "Please enter a valid email";
} elseif( email_exists( $email ) )
{
$errors['email'] = "This email address is already in use";
}
// Check password is valid
if(0 === preg_match("/.{6,}/", $_POST['password']))
{
$errors['password'] = "Password must be at least six characters";
}
// Check password confirmation_matches
if(0 !== strcmp($_POST['password'], $_POST['password_confirmation']))
{
$errors['password_confirmation'] = "Passwords do not match";
}
// Check terms of service is agreed to
if($_POST['terms'] != "Yes")
{
$errors['terms'] = "You must agree to Terms of Service";
}
if(0 === count($errors))
{
$password = $_POST['password'];
$new_user_id = wp_create_user( $username, $password, $email );
// You could do all manner of other things here like send an email to the user, etc. I leave that to you.
$success = 1;
//header( 'Location:' . get_bloginfo('url') . '/login/?success=1&u=' . $username );
}
}
}
?>
<form id="wp_signup_form" action="<?php echo $_SERVER['REQUEST_URI']; ?>" method="post">
<label for="username">Username</label>
<input type="text" name="username" id="username"> <br/>
<label for="email">Email address</label>
<input type="text" name="email" id="email"> <br/>
<label for="password">Password</label>
<input type="password" name="password" id="password"><br/>
<label for="password_confirmation">Confirm Password</label>
<input type="password" name="password_confirmation" id="password_confirmation"> <br/>
<input name="terms" id="terms" type="checkbox" value="Yes"> <br/>
<label for="terms">I agree to the Terms of Service</label>
<input type="submit" id="submitbtn" name="submit" value="Sign Up" />
</form>
<?php get_footer(); ?>
Blog Feed
add Active Class on product Categary
<?php
$args = array(
'taxonomy' => 'product_cat',
'hide_empty' => true,
'parent' => 0
);
$now_term = get_queried_object();
$terms = get_terms( $args );
if ( $terms ) {
echo '<div class="shop-header">';
echo '<div class="container">';
echo '<ul class="shop-menu">';
echo '<li>Categories: </li>';
$classs_all = empty($now_term) ? 'active' : '' ;
echo '<li class="' . $classs_all . '"><a href="'. site_url() .'/shop">All</a></li>';
foreach ( $terms as $term ) {
$classs = $now_term->slug == $term->slug ? 'active' : '' ;
echo '<li class="' . $classs . '"> <a href="' . esc_url( get_term_link( $term ) ) . '" class="' . $term->slug . '">'. $term->name . '</a> </li>';
}
echo '</ul>';
echo '</div> </div>';
}
?>
Add custom field to the checkout page
add_action('woocommerce_after_order_notes', 'custom_checkout_field');
function custom_checkout_field($checkout)
{
echo '<div id="custom_checkout_field"><h2>' . __('New Heading') . '</h2>';
woocommerce_form_field('custom_field_name', array(
'type' => 'text',
'class' => array(
'my-field-class form-row-wide'
) ,
'label' => __('Custom Additional Field') ,
'placeholder' => __('New Custom Field') ,
) ,
$checkout->get_value('custom_field_name'));
echo '</div>';
}
Add Submenu class
class My_Walker_Nav_Menu extends Walker_Nav_Menu {
function start_lvl(&$output, $depth = 0, $args = Array() ) {
$indent = str_repeat("\t", $depth);
$output = preg_replace( "/(.*)(\<li.*?class\=\")([^\"]*)(\".*?)$/", "$1$2$3 dropdown$4", $output );
$output .= "\n$indent<ul class=\"sub-menu\">\n";
}
}
Add Menu class on li
function add_additional_class_on_li($classes, $item, $args) {
if(isset($args->add_li_class)) {
$classes[] = $args->add_li_class;
}
return $classes;
}
add_filter('nav_menu_css_class', 'add_additional_class_on_li', 1, 3);
Breadcrumb Code
function get_breadcrumb() {
echo '<a href="'.home_url().'" rel="nofollow">Home</a>';
if (is_category() || is_single()) {
echo "|;» |;";
the_category(' • ');
if (is_single()) {
echo " » ";
the_title();
}
} elseif (is_page()) {
echo " / ";
echo the_title();
} elseif (is_search()) {
echo " » Search Results for... ";
echo '"<em>';
echo the_search_query();
echo '</em>"';
}
}
<?php get_breadcrumb(); ?>
Share Icon
<?php
$wplogoutURL = urlencode(get_the_permalink());
$wplogoutTitle = urlencode(get_the_title());
$wplogoutImage= urlencode(get_the_post_thumbnail_url(get_the_ID(), 'full'));
?>
<div class="share-icon">
<span>Share:</span>
<ul>
<li>
<a href="https://www.facebook.com/sharer/sharer.php?u=<?php echo $wplogoutURL; ?>" target="_blank"><i class="fa fa-facebook"></i></a>
</li>
<li>
<a href="https://twitter.com/intent/tweet?text=<?php echo $wplogoutTitle;?>&url=<?php echo $wplogoutURL;?>" target="_blank"><i class="fa fa-twitter"></i></a>
</li>
<li>
<a href="https://www.linkedin.com/shareArticle?url=<?php echo $wplogoutURL; ?>" target="_blank"><i class="fa fa-linkedin"></i></a>
</li>
<!-- <li>
<a href="<?php// site_url();?>"><i class="fa fa-instagram"></i></a>
</li>
<li>
<a href="<?php //site_url();?>"><i class="fa fa-google-plus"></i></a>
</li> -->
</ul>
</div>
theme customizer panel
function owt_custom_theme_customize_register($wp_customize) {
$wp_customize->add_section('owt_main_section', array(
'title' => "Online Web tutor Section",
'description' => '',
'priority' => 120,
));
// =============================
// = Footer Text =
// =============================
$wp_customize->add_setting('owt_first_footer_setting', array(
'default' => 'Designed and developed by Online Web Tutor',
'capability' => 'edit_theme_options',
'type' => 'option',
));
$wp_customize->add_control('owt_first_footer_control', array(
'label' => "Footer text",
'section' => 'owt_main_section',
'settings' => 'owt_first_footer_setting',
));
$wp_customize->add_setting('owt_first_footer_link', array(
'default' => 'Online Web tutor',
'capability' => 'edit_theme_options',
'type' => 'option',
));
$wp_customize->add_control('owt_first_footer_link_control', array(
'label' => "Footer Link",
'section' => 'owt_main_section', // type="text"
'settings' => 'owt_first_footer_link',
));
$wp_customize->add_setting('owt_setting_footer_link', array(
'capability' => 'edit_theme_options',
'type' => 'option',
));
$wp_customize->add_control('owt_footer_pages_control', array(
'label' => "Footer Link",
'section' => 'owt_main_section',
'type' => 'dropdown-pages',
'settings' => 'owt_setting_footer_link',
));
$wp_customize->add_setting('owt_image_uploader', array(
'default' => get_bloginfo("template_url").'/images/category4.png',
'capability' => 'edit_theme_options',
'type' => 'option',
));
$wp_customize->add_control( new WP_Customize_Image_Control($wp_customize, 'image_upload_test', array(
'label' => "Choose Image",
'section' => 'owt_main_section',
'settings' => 'owt_image_uploader',
)));
$wp_customize->add_setting('owt_color_picker_id', array(
'default' => '#000',
'sanitize_callback' => 'sanitize_hex_color',
'capability' => 'edit_theme_options',
'type' => 'option',
));
$wp_customize->add_control( new WP_Customize_Color_Control($wp_customize, 'link_color', array(
'label' => "Book Now Background",
'section' => 'owt_main_section',
'settings' => 'owt_color_picker_id',
)));
$wp_customize->add_setting( 'themeslug_textarea_setting_id', array(
'capability' => 'edit_theme_options',
'default' => 'Lorem Ipsum Dolor Sit amet',
'sanitize_callback' => 'sanitize_textarea_field',
) );
$wp_customize->add_control( 'themeslug_textarea_setting_id', array(
'type' => 'textarea',
'section' => 'owt_main_section', // // Add a default or your own section
'label' => __( 'Custom Text Area' ),
'description' => __( 'This is a custom textarea.' ),
) );
}
add_action('customize_register', 'owt_custom_theme_customize_register');
WordPress Ajax Search Without Plugin
// the ajax function
add_action('wp_ajax_data_fetch' , 'data_fetch');
add_action('wp_ajax_nopriv_data_fetch','data_fetch');
function data_fetch(){
$the_query = new WP_Query( array( 'posts_per_page' => 5, 's' => esc_attr( $_POST['keyword'] ), 'post_type' => 'post' ) );
if( $the_query->have_posts() ) :
while( $the_query->have_posts() ): $the_query->the_post(); ?>
<a href="<?php echo esc_url( post_permalink() ); ?>"><?php the_title();?></a>
<?php endwhile;
wp_reset_postdata();
else:
echo '<h3>No Results Found</h3>';
endif;
die();
}
// add the ajax fetch js
add_action( 'wp_footer', 'ajax_fetch' );
function ajax_fetch() {
?>
<script type="text/javascript">
function fetchResults(){
var keyword = jQuery('#searchInput').val();
if(keyword == ""){
jQuery('#datafetch').html("");
} else {
jQuery.ajax({
url: '<?php echo admin_url('admin-ajax.php'); ?>',
type: 'post',
data: { action: 'data_fetch', keyword: keyword },
success: function(data) {
jQuery('#datafetch').html( data );
}
});
}
}
</script>
<?php
}
Custom post full Code
add_action('init', 'portfolio_custom_init');
/*-- Custom Post Init Begin --*/
function portfolio_custom_init()
{
$labels = array(
'name' => _x('Portfolio', 'post type general name'),
'singular_name' => _x('Portfolio', 'post type singular name'),
'add_new' => _x('Add New', 'Portfolio'),
'add_new_item' => __('Add New Portfolio'),
'edit_item' => __('Edit Portfolio'),
'new_item' => __('New Portfolio'),
'view_item' => __('View Portfolio'),
'search_items' => __('Search Portfolio'),
'not_found' => __('No Portfolio found'),
'not_found_in_trash' => __('No Portfolio found in Trash'),
'parent_item_colon' => '',
'menu_name' => 'Portfolio'
);
$args = array(
'labels' => $labels,
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'show_in_menu' => true,
'query_var' => true,
'rewrite' => true,
'capability_type' => 'post',
'has_archive' => true,
'hierarchical' => false,
'menu_position' => null,
'supports' => array('title','editor','author','thumbnail','excerpt','comments')
);
// The following is the main step where we register the post.
register_post_type('portfolio',$args);
// Initialize New Taxonomy Labels
$labels = array(
'name' => _x( 'Portfolio Category', 'taxonomy general name' ),
'singular_name' => _x( 'Tag', 'taxonomy singular name' ),
'search_items' => __( 'Search Types' ),
'all_items' => __( 'All Tags' ),
'parent_item' => __( 'Parent Tag' ),
'parent_item_colon' => __( 'Parent Tag:' ),
'edit_item' => __( 'Edit Tags' ),
'update_item' => __( 'Update Tag' ),
'add_new_item' => __( 'Add New Tag' ),
'new_item_name' => __( 'New Tag Name' ),
);
// Custom taxonomy for Project Tags
register_taxonomy('portfolio_category',array('portfolio'), array(
'hierarchical' => true,
'labels' => $labels,
'show_ui' => true,
'query_var' => true,
'rewrite' => array( 'slug' => 'portfolio_category' ),
));
}
/*-- Custom Post Init End --*/