11 July 2020

How to check if page is exists or not in wordpress ?

check if page is exists or not in wordpress.


function pagecheck(){

$pagetitle = 'Sample Pagee';
$page = get_page_by_title( $pagetitle, OBJECT, 'page' );
$pageid = $page->ID;

if($pageid){
//echo "Exist";
}
else
{

echo "not Exist";

}

}
add_action('admin_head','pagecheck');

How to create wordpress page with code in wordpress?

Create wordpress page with code in wordpress.Define page template in wp_insert_post.

add_action( 'admin_init', 'mytheme_admin_init' );
function mytheme_admin_init() {
    if ( ! get_option( 'mytheme_installed' ) ) {
        $new_page_id = wp_insert_post( array(
            'post_title'     => 'Blog',
            'post_type'      => 'page',
            'post_name'      => 'blog',
            'comment_status' => 'closed',
            'ping_status'    => 'closed',
            'post_content'   => '',
            'post_status'    => 'publish',
            'post_author'    => get_user_by( 'id', 1 )->user_id,
            'menu_order'     => 0,
            // Assign page template
            'page_template'  => 'template-blog.php'
        ) );

        update_option( 'mytheme_installed', true );
    }
}
In working...........
add_action( 'admin_init', 'mytheme_admin_init' );
function mytheme_admin_init() {
    if ( ! get_option( 'front page' ) ) {
        $new_page_id = wp_insert_post( array(
            'post_title'     => 'front page',
            'post_type'      => 'page',
            'post_name'      => 'front page',
            'comment_status' => 'closed',
            'ping_status'    => 'closed',
            'post_content'   => '',
            'post_status'    => 'publish',
            'post_author'    => 1,
            'menu_order'     => 0,
            // Assign page template
            //'page_template'  => 'template-jassi.php'
        ) );

        update_option( 'front page', true );
    }
 
      if ( ! get_option( 'Contact Us' ) ) {
        $new_page_id = wp_insert_post( array(
            'post_title'     => 'Contact Us',
            'post_type'      => 'page',
            'post_name'      => 'Contact Us',
            'comment_status' => 'closed',
            'ping_status'    => 'closed',
            'post_content'   => '',
            'post_status'    => 'publish',
            'post_author'    => get_user_by( 'id', 1 )->user_id,
            'menu_order'     => 0,
            // Assign page template
            //'page_template'  => 'template-jassi.php'
        ) );

        update_option( 'Contact Us', true );
    }
 
 
      if ( ! get_option( 'About Us' ) ) {
        $new_page_id = wp_insert_post( array(
            'post_title'     => 'About Us',
            'post_type'      => 'page',
            'post_name'      => 'About Us',
            'comment_status' => 'closed',
            'ping_status'    => 'closed',
            'post_content'   => '',
            'post_status'    => 'publish',
            'post_author'    => get_user_by( 'id', 1 )->user_id,
            'menu_order'     => 0,
            // Assign page template
            //'page_template'  => 'template-jassi.php'
        ) );

        update_option( 'About Us', true );
    }
 
 
      if ( ! get_option( 'Privacy Policy' ) ) {
        $new_page_id = wp_insert_post( array(
            'post_title'     => 'Privacy Policy',
            'post_type'      => 'page',
            'post_name'      => 'Privacy Policy',
            'comment_status' => 'closed',
            'ping_status'    => 'closed',
            'post_content'   => '',
            'post_status'    => 'publish',
            'post_author'    => get_user_by( 'id', 1 )->user_id,
            'menu_order'     => 0,
            // Assign page template
            //'page_template'  => 'template-jassi.php'
        ) );

        update_option( 'Privacy Policy', true );
    }
 
 
     if ( ! get_option( 'term and condition' ) ) {
        $new_page_id = wp_insert_post( array(
            'post_title'     => 'term and condition',
            'post_type'      => 'page',
            'post_name'      => 'term and condition',
            'comment_status' => 'closed',
            'ping_status'    => 'closed',
            'post_content'   => '',
            'post_status'    => 'publish',
            'post_author'    => get_user_by( 'id', 1 )->user_id,
            'menu_order'     => 0,
            // Assign page template
            //'page_template'  => 'template-jassi.php'
        ) );

        update_option( 'term and condition', true );
    }
 
 
   if ( ! get_option( 'Test' ) ) {
        $new_page_id = wp_insert_post( array(
            'post_title'     => 'Test',
            'post_type'      => 'page',
            'post_name'      => 'Test',
            'comment_status' => 'closed',
            'ping_status'    => 'closed',
            'post_content'   => '',
            'post_status'    => 'publish',
            'post_author'    => get_user_by( 'id', 1 )->user_id,
            'menu_order'     => 0,
            // Assign page template
            //'page_template'  => 'template-jassi.php'
        ) );

        update_option( 'Test', true );
    }
}

06 July 2020

How to create custom meta box inside custom post type in wordpress?

Create custom meta box inside custom post type in wordpress.



<?php

/* custom meta box */

function flipalbums_add_meta_boxes( $post ){
add_meta_box( 'flipalbum_meta_box', __( 'Extras', 'flipalbum_audio_plugin' ), 'flipalbum_audio_meta_box', 'flipalbums', 'normal', 'low' );
}
add_action( 'add_meta_boxes_flipalbums', 'flipalbums_add_meta_boxes' );



function flipalbum_audio_meta_box( $post ){
// make sure the form request comes from WordPress
wp_nonce_field( basename( __FILE__ ), 'flipalbum_meta_box_nonce' );

// retrieve the _food_cholesterol current value
//$current_cholesterol = get_post_meta( $post->ID, '_food_cholesterol', true );

// retrieve the _flip_music_file current value
$current_music = get_post_meta( $post->ID, '_flip_music_file', true );

$customer_email = get_post_meta( $post->ID, '_flip_customer_email', true );

$customer_pin = get_post_meta( $post->ID, '_flip_customer_pin', true );

//$vitamins = array( 'Vitamin A', 'Thiamin (B1)', 'Riboflavin (B2)', 'Niacin (B3)', 'Pantothenic Acid (B5)', 'Vitamin B6', 'Vitamin B12', 'Vitamin C', 'Vitamin D', 'Vitamin E', 'Vitamin K' );

// stores _food_vitamins array
//$current_vitamins = ( get_post_meta( $post->ID, '_food_vitamins', true ) ) ? get_post_meta( $post->ID, '_food_vitamins', true ) : array();

?>
<div class='inside'>

<!--<h3><?php _e( 'Cholesterol', 'flipalbum_audio_plugin' ); ?></h3>
<p>
<input type="radio" name="cholesterol" value="0" <?php checked( $current_cholesterol, '0' ); ?> /> Yes<br />
<input type="radio" name="cholesterol" value="1" <?php checked( $current_cholesterol, '1' ); ?> /> No
</p>-->

<h3><?php _e( 'Music File Goes here', 'flipalbum_audio_plugin' ); ?></h3>
<p>
<input type="text" name="music" value="<?php echo $current_music; ?>" style="width:100%"/>
</p>

<!--<h3><?php _e( 'Vitamins', 'flipalbum_audio_plugin' ); ?></h3>
<p>
<?php
foreach ( $vitamins as $vitamin ) {
?>
<input type="checkbox" name="vitamins[]" value="<?php echo $vitamin; ?>" <?php checked( ( in_array( $vitamin, $current_vitamins ) ) ? $vitamin : '', $vitamin ); ?> /><?php echo $vitamin; ?> <br />
<?php
}
?>
</p>-->
</div>


<div class='inside'>
<h3><?php _e( 'User Email', 'flipalbum_audio_plugin' ); ?></h3>
<p>
<input type="text" name="custemail" value="<?php echo $customer_email; ?>" style="width:100%"/>
</p>


</div>
<div class='inside'>
<h3><?php _e( 'User PIN', 'flipalbum_audio_plugin' ); ?></h3>
<p>
<input type="text" name="custpin" value="<?php echo $customer_pin; ?>" style="width:100%"/>
</p>


</div>

<?php
}


function flipalbums_save_meta_boxes_data( $post_id ){
// verify taxonomies meta box nonce
if ( !isset( $_POST['flipalbum_meta_box_nonce'] ) || !wp_verify_nonce( $_POST['flipalbum_meta_box_nonce'], basename( __FILE__ ) ) ){
return;
}

// return if autosave
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ){
return;
}

// Check the user's permissions.
if ( ! current_user_can( 'edit_post', $post_id ) ){
return;
}

// store custom fields values
// cholesterol string
/* if ( isset( $_REQUEST['cholesterol'] ) ) {
update_post_meta( $post_id, '_food_cholesterol', sanitize_text_field( $_POST['cholesterol'] ) );
} */

// store custom fields values
// music string
if ( isset( $_REQUEST['music'] ) ) {
update_post_meta( $post_id, '_flip_music_file', sanitize_text_field( $_POST['music'] ) );
}

if ( isset( $_REQUEST['custemail'] ) ) {
update_post_meta( $post_id, '_flip_customer_email', sanitize_text_field( $_POST['custemail'] ) );
}
if ( isset( $_REQUEST['custpin'] ) ) {
update_post_meta( $post_id, '_flip_customer_pin', sanitize_text_field( $_POST['custpin'] ) );
}

// store custom fields values
/* if( isset( $_POST['vitamins'] ) ){
$vitamins = (array) $_POST['vitamins'];

// sinitize array
$vitamins = array_map( 'sanitize_text_field', $vitamins );

// save data
update_post_meta( $post_id, '_food_vitamins', $vitamins );
}else{
// delete data
delete_post_meta( $post_id, '_food_vitamins' );
} */
}
add_action( 'save_post_flipalbums', 'flipalbums_save_meta_boxes_data', 10, 2 );

How to register custom post in wordpress?

Register custom post in wordpress.



 
 // Register Custom Post Type
function three_d_albums_post_type() {

$labels = array(
'name'                  => _x( '3D Album', 'Post Type General Name', 'text_domain' ),
'singular_name'         => _x( '3D Albums', 'Post Type Singular Name', 'text_domain' ),
'menu_name'             => __( '3D Albums', 'text_domain' ),
'name_admin_bar'        => __( '3D Albums', 'text_domain' ),
'archives'              => __( 'Item Archives', 'text_domain' ),
'attributes'            => __( 'Item Attributes', 'text_domain' ),
'parent_item_colon'     => __( 'Parent Item:', 'text_domain' ),
'all_items'             => __( 'All Items', 'text_domain' ),
'add_new_item'          => __( 'Add New Item', 'text_domain' ),
'add_new'               => __( 'Add New', 'text_domain' ),
'new_item'              => __( 'New Item', 'text_domain' ),
'edit_item'             => __( 'Edit Item', 'text_domain' ),
'update_item'           => __( 'Update Item', 'text_domain' ),
'view_item'             => __( 'View Item', 'text_domain' ),
'view_items'            => __( 'View Items', 'text_domain' ),
'search_items'          => __( 'Search Item', 'text_domain' ),
'not_found'             => __( 'Not found', 'text_domain' ),
'not_found_in_trash'    => __( 'Not found in Trash', 'text_domain' ),
'featured_image'        => __( 'Featured Image', 'text_domain' ),
'set_featured_image'    => __( 'Set featured image', 'text_domain' ),
'remove_featured_image' => __( 'Remove featured image', 'text_domain' ),
'use_featured_image'    => __( 'Use as featured image', 'text_domain' ),
'insert_into_item'      => __( 'Insert into item', 'text_domain' ),
'uploaded_to_this_item' => __( 'Uploaded to this item', 'text_domain' ),
'items_list'            => __( 'Items list', 'text_domain' ),
'items_list_navigation' => __( 'Items list navigation', 'text_domain' ),
'filter_items_list'     => __( 'Filter items list', 'text_domain' ),
);
$args = array(
'label'                 => __( '3D Albums', 'text_domain' ),
'description'           => __( 'Post Type Description', 'text_domain' ),
'labels'                => $labels,
'supports'              => array( 'title', 'editor', 'thumbnail', 'custom-fields' ),
'taxonomies'            => array( 'category', 'post_tag' ),
'hierarchical'          => false,
'public'                => true,
'show_ui'               => true,
'show_in_menu'          => true,
'menu_position'         => 5,
'show_in_admin_bar'     => true,
'show_in_nav_menus'     => true,
'can_export'            => true,
'has_archive'           => true,
'exclude_from_search'   => true,
'publicly_queryable'    => true,
'capability_type'       => 'page',
);
register_post_type( '3D_Albums', $args );

}
add_action( 'init', 'three_d_albums_post_type', 0 );

How to create menu on my account page in woocommerce?

create menu on my account page in woocommerce.


/* my account add code */


function my_albumspoints() {
add_rewrite_endpoint( 'my-albums', EP_ROOT | EP_PAGES );
}

add_action( 'init', 'my_albumspoints' );

/**
 * Add new query var.
 *
 * @param array $vars
 * @return array
 */
function my_custom_query_vars( $vars ) {
$vars[] = 'my-albums';

return $vars;
}

add_filter( 'query_vars', 'my_custom_query_vars', 0 );


function my_custom_flush_rewrite_rules() {
add_rewrite_endpoint( 'my-albums', EP_ROOT | EP_PAGES );
flush_rewrite_rules();
}

add_action( 'after_switch_theme', 'my_custom_flush_rewrite_rules' );



function my_custom_insert_after_helper( $items, $new_items, $after ) {
// Search for the item position and +1 since is after the selected item key.
$position = array_search( $after, array_keys( $items ) ) + 0;

// Insert the new item.
$array = array_slice( $items, 0, $position, true );
$array += $new_items;
$array += array_slice( $items, $position, count( $items ) - $position, true );

    return $array;
}

/* My Albums in My Account  */
function my_custom_my_account_menu_items( $items ) {
$new_items = array();
$new_items['my-albums'] = __( 'My Albums', 'woocommerce' );

// Add the new item after `orders`.
return my_custom_insert_after_helper( $items, $new_items, 'orders' );
}

add_filter( 'woocommerce_account_menu_items', 'my_custom_my_account_menu_items' );


function my_albumspoint_content() {
echo do_shortcode('[total-albums]');
}
add_action( 'woocommerce_account_my-albums_endpoint', 'my_albumspoint_content' );



function my_albumspoint_title( $title ) {
global $wp_query;

$is_endpoint = isset( $wp_query->query_vars['my-albums'] );

if ( $is_endpoint && ! is_admin() && is_main_query() && in_the_loop() && is_account_page() ) {
// New page title.
$title = __( 'My Albums', 'woocommerce' );

remove_filter( 'the_title', 'my_albumspoint_title' );
}

return $title;
}

add_filter( 'the_title', 'my_albumspoint_title' );





20 June 2020

How create and show meta (custom) fields in wordpress posts ?

How create meta (custom) fields in wordpress posts ?




<?php


/**
 * Register meta boxes.
 */
function meta_register_meta_boxes() {
    add_meta_box( 'meta-1', __( 'Custom Field', 'meta' ), 'meta_display_callback', 'contracts' );
}
add_action( 'add_meta_boxes', 'meta_register_meta_boxes' );

/**
 * Meta box display callback.
 *
 * @param WP_Post $post Current post object.
 */
function meta_display_callback( $post ) {
?>


<div class="meta_box">
    <style scoped>
        .meta_box{
            display: grid;
            grid-template-columns: max-content 1fr;
            grid-row-gap: 10px;
            grid-column-gap: 20px;
        }
        .meta_field{
            display: contents;
        }
    </style>
    <p class="meta-options meta_field">
        <label for="meta_author">Author</label>
        <input id="meta_author"
            type="text"
            name="meta_author"
            value="<?php echo esc_attr( get_post_meta( get_the_ID(), 'meta_author', true ) ); ?>">
    </p>
    <p class="meta-options meta_field">
        <label for="meta_published_date">Published Date</label>
        <input id="meta_published_date"
            type="date"
            name="meta_published_date"
           value="<?php echo esc_attr( get_post_meta( get_the_ID(), 'meta_published_date', true ) ); ?>">
    </p>
    <p class="meta-options meta_field">
        <label for="meta_price">Price</label>
        <input id="meta_price"
            type="number"
            name="meta_price"
            value="<?php echo esc_attr( get_post_meta( get_the_ID(), 'meta_price', true ) ); ?>">
    </p>
</div>
<?php
}




/**
 * Save meta box content.
 *
 * @param int $post_id Post ID
 */
function meta_save_meta_box( $post_id ) {
    if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;
    if ( $parent_id = wp_is_post_revision( $post_id ) ) {
        $post_id = $parent_id;
    }
    $fields = [
        'meta_author',
        'meta_published_date',
        'meta_price',
    ];
    foreach ( $fields as $field ) {
        if ( array_key_exists( $field, $_POST ) ) {
            update_post_meta( $post_id, $field, sanitize_text_field( $_POST[$field] ) );
        }
     }
}
add_action( 'save_post', 'meta_save_meta_box' );





How to add custom woocommerce-MyAccount-navigation on woocommerce account page






// Note the low hook priority, this should give to your other plugins the time to add their own items...
add_filter( 'woocommerce_account_menu_items', 'add_my_menu_items', 99, 1 );

function add_my_menu_items( $items ) {
    $my_items = array(
    //  endpoint   => label
        '2nd-item' => __( '2nd Item', 'my_plugin' ),
        '3rd-item' => __( '3rd Item', 'my_plugin' ),
    );

    $my_items = array_slice( $items, 0, 1, true ) +
        $my_items +
        array_slice( $items, 1, count( $items ), true );

    return $my_items;
}

How to create video slider with cuncks of videos without download all videos?

 Create video slider with cuncks of videos without download all videos function custom_video_slider_shortcode() { $slider_id = 'vide...