Tuesday, October 30, 2018

Wordpress create custom fields into Settings -> General

<?php
add_filter('admin_init', 'notification_start_date');
function notification_start_date() {
    register_setting('general', 'notification_start_date', 'esc_attr');
    add_settings_field('notification_start_date', '<label for="notification_start_date">' . __('Notification start date', 'notification_start_date') . '</label>', 'notification_start_date_html', 'general');
}
function notification_start_date_html() {
    $value = get_option('notification_start_date', '');
    echo '<input type="date" id="notification_start_date" name="notification_start_date" value="' . $value . '" />';
}




add_filter('admin_init', 'notification_end_date');
function notification_end_date() {
    register_setting('general', 'notification_end_date', 'esc_attr');
    add_settings_field('notification_end_date', '<label for="notification_end_date">' . __('Notification start date', 'notification_end_date') . '</label>', 'notification_end_date_html', 'general');
}
function notification_end_date_html() {
    $value = get_option('notification_end_date', '');
    echo '<input type="date" id="notification_end_date" name="notification_end_date" value="' . $value . '" />';
}



add_filter('admin_init', 'notification');
function notification() {
    register_setting('general', 'notification', 'esc_attr');
    add_settings_field('notification', '<label for="notification">' . __('Notification start date', 'notification') . '</label>', 'notification_html', 'general');
}
function notification_html() {
    $value = get_option('notification', '');
    echo '<input type="date" id="notification" name="notification" value="' . $value . '" />';
}
?>

Wordpress create post with categories

add_action( 'init', 'article_fn' );

function article_fn() {

register_post_type( 'article', array(

  'labels' => array(

    'name' => 'Article',

    'add_new_item' => 'Add new article',

    'add_new' => 'Add new article',

    'view_item' => 'View Article',

    'singular_name' => 'Article',

    'set_featured_image' => 'Set article featured image',

    'search_items' => 'Search Article',

    'menu_name' => 'All Articles',

    'name_admin_bar' => 'View all articles'

   ),

  'description' => 'Article which we will be discussing on this blog.',

  'public' => true,

  'taxonomies' => array( 'category' ),

  'menu_position' => 2,

  'supports' => array( 'title', 'editor', 'custom-fields','author','thumbnail','excerpt','trackbacks','comments','revisions','page-attributes','post-formats' )

));

}

Wordpress custom posttype

add_theme_support('post-thumbnails');
add_post_type_support( 'my_product', 'thumbnail' );   
function create_post_type() {
        register_post_type( 'my_product',
            array(
                'labels' => array(
                    'name' => __( 'Products' ),
                    'singular_name' => __( 'Product' )
                ),
                'public' => true,
                'has_archive' => true
            )
        );
    }
    add_action( 'init', 'create_post_type' );

Hide wordpress current version details on browser.

function dd_remove_wp_version_strings($src) {

        global $wp_version;

        parse_str(parse_url($src, PHP_URL_QUERY), $query);

        if (!empty($query['ver']) && $query['ver'] === $wp_version) {

            $src = remove_query_arg('ver', $src);

        }

        return $src;

    }

    add_filter('script_loader_src', 'dd_remove_wp_version_strings');

    add_filter('style_loader_src', 'dd_remove_wp_version_strings');

    function dd_remove_version() {

        return '';

    }

    add_filter('the_generator', 'dd_remove_version');