Friday, March 10, 2017

How to move (alert) sucess message for Contact Form 7 to top of form in WordPress

You can use the [response] tag in your form to change the location of the response message.

For example:


[response]

<p>Your Name (required)<br />
    [text* your-name] </p>

<p>Your Email (required)<br />
    [email* your-email] </p>

<p>Subject<br />
    [text your-subject] </p>

<p>Your Message<br />
    [textarea your-message] </p>

<p>[submit "Send"]</p>

Wednesday, March 1, 2017

How to add Password Custom field in Contact form 7.

First of all add this below code to your theme’s functions.php file.
function cfp($atts, $content = null) {
    extract(shortcode_atts(array( "id" => "", "title" => "", "pwd" => "" ), $atts));

    if(empty($id) || empty($title)) return "";

    $cf7 = do_shortcode('[contact-form-7 404 "Not Found"]');

    $pwd = explode(',', $pwd);
    foreach($pwd as $p) {
        $p = trim($p);

        $cf7 = preg_replace('/<input type="text" name="' . $p . '"/usi', '<input type="password" name="' . $p . '"', $cf7);
    }

    return $cf7;
}
add_shortcode('cfp', 'cfp');
Now we create a form with contact form 7 plugin with the following three fields: usernameemail and password
<p>Your Username (required)<br />
   [text* username] </p>

<p>Your Email (required)<br />
  [email* email] </p>

<p>Your Password<br />
  [text* password] </p>

<p>[submit "Register"]</p>
Now if we hide password field like (***) of the contact form then write following short code into your post:
[cfp id="7" title="Test" pwd="password"]

Friday, July 8, 2016

Wordpress Codex

1. List of taxonomy
<?php
//list terms in a given taxonomy
$taxonomy = 'industry';
$tax_terms = get_terms($taxonomy);
?>
<ul>
<?php
foreach ($tax_terms as $tax_term) {
echo '<li>' . '<a href="' . esc_attr(get_term_link($tax_term, $taxonomy)) . '" title="' . sprintf( __( "View all posts in %s" ), $tax_term->name ) . '" ' . '>' . $tax_term->name.'</a></li>';
}
?>
</ul>

2. List of all categories for post
<?php wp_list_categories(); ?>

3. List of  All Post types

<?php
foreach ( get_post_types( '', 'names' ) as $post_type ) {
   echo '<p>' . $post_type . '</p>';
}
?>


4.List of all Post for custom Post type.

$query = new WP_Query( array( 'post_type' => 'book' ) );
while ( $query->have_posts() ) : $query->the_post();
echo '<li><a href="';
the_permalink();
echo '">';
the_title();
echo '</a></li>';
endwhile;

5. Important Template Files:

    header.php (displays the header area)
    footer.php (displays the footer area)
    index.php (shows your latest blog posts)
    single.php (shows a single blog post)
    page.php (displays static page)
    sidebar.php (displays the sidebar)
    archive.php (shows blog archives)

Thursday, April 23, 2015

What are the steps you can take if your WordPress file is hacked?

  • Install security plugin like WP security
  • Re-install the latest version of WordPress
  • Change password and user-ids for all your users
  • Check your themes and plugins are up to date
  • What are rules to follow in WordPress plugin development?

  • Find a unique name
  • Setup a prefix (related to your brand)
  • Create the plugin’s folder
  • Create sub-folders for PHP files, assets, and translations
  • Create the main plugin file and fill in obligatory header information
  • Create a readme.txt file
  • Use proper constants and functions to detect paths to plugin files
  • Create additional PHP files and include them inside the main one
  • Create activation and deactivation functions
  • Create an uninstall script
  • What are the template tags in WordPress?

    A template tag is code that instructs WordPress to “do” or “get” something. Like in header.php  we will use the tag bloginfo(‘name’) to get “Site Title” from wp-options table which is set in Setting > General at WordPress dashboard.
    The the_title() template tag is used to display the post title.
    wp_list_cats() is  for display categories.
    get_header() for getting header.
    get_sidebar() for display the sidebar on page.
    get_footer() for get the footer content on page.

    Monday, April 20, 2015

    Creating a Custom Post Type

    Once you’ve identified that you need a custom post type, there are two ways to go about it: coding it yourself or using a plugin. I’ll demonstrate the code necessary to register a custom post type in a theme or plugin shortly but first let’s look at some of the plugins you can use:
    • Our own CustomPress provides you with an interface for creating custom post types, taxonomies and custom fields and is very user-friendly. If you want to create many types of custom content, this will save you installing more than one plugin.
    • The Custom Post Type UI plugin is the most popular free plugin for adding custom post types on the plugin repository. It lets you add custom post types and taxonomies, but not custom fields. The interface isn’t quite as user-friendly as with CustomPress, with more technical terminology for you to get your head round, but it does the job.
    You can read more about plugins to help you create custom content in our review of the best CMS plugins.
    But if you’re comfortable with coding your own, here’s how you go about registering a custom post type.

    Coding Your Post Type

    WordPress provides a function called register_post_type() which you use to create your custom post type. You add the function to your theme’s functions file or (even better) to a plugin you create specially, and then fire it via the init hook in WordPress.
    Note: It’s better to register your custom post type in a plugin rather than your theme because then if you change your theme in the future, you won’t lose your custom post type.
    To follow this technique you’ll need access to your WordPress files and a code editor. I recommend trying this out in a development site and not on your live site!
    Start by creating a new empty file called post-types.php and save it to the plugins folder in your wp-content directory.
    Now add the following code to your file:
    12345678
    <?php
    /*Plugin Name: Create Product Post Type
    Description: This plugin registers the 'product' post type.
    Version: 1.0
    License: GPLv2
    */
     
    ?>
    This opens the plugin file and tells WordPress that it’s a plugin. It also provides a title and a description which will help you identify the plugin when you activate it in your site.
    Next, below the line that reads */ and above the closing ?> line, add these lines:
    1234
    function wpmudev_create_post_type() {
     
    }
    add_action( 'init', 'wpmudev_create_post_type' );
    This creates a function which will hold the code to register your post type, and then attaches it to the init hook using the add_action() function. This makes sure that WordPress runs your function at the right time.
    Now you need to add the code for your custom post type inside the curly braces:
    12345678910111213141516171819202122232425262728
    // set up labels
    $labels = array(
    'name' => 'Products',
    'singular_name' => 'Product',
    'add_new' => 'Add New Product',
    'add_new_item' => 'Add New Product',
    'edit_item' => 'Edit Product',
    'new_item' => 'New Product',
    'all_items' => 'All Products',
    'view_item' => 'View Product',
    'search_items' => 'Search Products',
    'not_found' => 'No Products Found',
    'not_found_in_trash' => 'No Products found in Trash',
    'parent_item_colon' => '',
    'menu_name' => 'Products',
    );
    //register post type
    register_post_type( 'product', array(
    'labels' => $labels,
    'has_archive' => true,
    'public' => true,
    'supports' => array( 'title', 'editor', 'excerpt', 'custom-fields', 'thumbnail','page-attributes' ),
    'taxonomies' => array( 'post_tag', 'category' ),
    'exclude_from_search' => false,
    'capability_type' => 'post',
    'rewrite' => array( 'slug' => 'products' ),
    )
    );
    view raw1502 CPT inner code hosted with ❤ by GitHub
    Let’s take a close look at what this code does. The first section tells WordPress what labels you want to use for your post type. These will replace the default labels which are ‘Posts’, ‘Add Post’ etc. Next you call the register_post_type() function, which has the following parameters:
    • title: your function won’t work without this.
    • labels: the labels you already specified.
    • has_archive: by setting this to true you’re telling WordPress that it’s possible to show archive pages for this post type.
    • public: set this to true to make sure people can see a page on your website for each product.
    • supports: the elements of the WordPress admin that the custom post type supports.
    • taxonomies: an array of the taxonomies you want to use with the post type. I’ve used the inbuilt categories and tags, although you might want to leave this blank and register a custom taxonomy for your post type later on.
    • exclude_from_search: set this to false to make sure your products are included in search results.
    • capability_type: this is set as post as our products will behave like posts and not pages.
    • rewrite: this tells WordPress what the slug is for the post type archive, which is the text to use after your domain name for its URL. By setting this to products, the URL for the products page will be http://mysite.com/products/
    Now save your file. The entire plugin’s code will look like this:
    12345678910111213141516171819202122232425262728293031323334353637383940
    <?php
    /*Plugin Name: Create Product Post Type
    Description: This plugin registers the 'product' post type.
    Version: 1.0
    License: GPLv2
    */
     
    // register custom post type to work with
    function wpmudev_create_post_type() {
    // set up labels
    $labels = array(
    'name' => 'Products',
    'singular_name' => 'Product',
    'add_new' => 'Add New Product',
    'add_new_item' => 'Add New Product',
    'edit_item' => 'Edit Product',
    'new_item' => 'New Product',
    'all_items' => 'All Products',
    'view_item' => 'View Product',
    'search_items' => 'Search Products',
    'not_found' => 'No Products Found',
    'not_found_in_trash' => 'No Products found in Trash',
    'parent_item_colon' => '',
    'menu_name' => 'Products',
    );
    //register post type
    register_post_type( 'product', array(
    'labels' => $labels,
    'has_archive' => true,
    'public' => true,
    'supports' => array( 'title', 'editor', 'excerpt', 'custom-fields', 'thumbnail','page-attributes' ),
    'taxonomies' => array( 'post_tag', 'category' ),
    'exclude_from_search' => false,
    'capability_type' => 'post',
    'rewrite' => array( 'slug' => 'products' ),
    )
    );
    }
    add_action( 'init', 'wpmudev_create_post_type' );
    ?>
    For your post type to work you’ll need to activate your new plugin. Go to the Plugins page in your site admin and find your new plugin:
    activate-cpt-plugin
    admin-menu-with-new-post-type
    Click the Activate link to activate it. You’ll now see an extra post type in your admin menu:
    You can click on the Products link and start adding products to your site in just the same way as you would posts.

    Displaying Your Post Type on the Front End

    Once you’ve added some products, you’ll need to be able to display these on the front end of your site.

    Setting Permalinks to Link to Your Post Type’s Pages

    First you need to refresh the permalinks on your site so that WordPress will use the correct links to display products or product archives:
    1. In the WordPress admin, go to Settings -> Permalinks.
    2. Make sure the ‘post name’ option is selected.
    3. Click the Save Changes button.
    Note: You need to do this even if you’ve already configured permalinks, as WordPress needs to refresh these settings for the new set of links to your products.

    Adding Your Post Type to the Navigation Menu

    If you want to add products to your navigation menu, you can. Go to Appearance -> Menus, and you’ll see that Products are displayed on the left. Simply drag any products onto your menu to include them.
    Adding your product archive is a bit trickier, and for this you’ll need to add a custom link to the menu.
    1. Still on the Menus screen, click on the Links box to the left.
    2. In the URL field, type http://mysite.com/products/, replacing mysite.com with your own domain.
    3. In the Link text field, type Products.
    4. Click the Add to Menu button.
    5. Once the link has been added to the menu, move it to the right place and save the menu.
    6. Save your changes by clicking Save MenuDon’t miss this step!
    Now if you visit your site’s front end and click on that menu link, you’ll be take to your product archive page. Here’s mine:
    product-archive-page
    So now you have a page to display all of your products. But what if you want to customize the way you display them?

    A Template For Your Post Type

    WordPress uses something called the template hierarchy to identify which template file in your theme it should use to display a given content type. It’s beyond the scope of this post to describe how the template hierarchy works in detail, but it helps to know how WordPress displays your new post type.
    When WordPress displays a single product, it will look for each of these files in order in your theme:
    1. A file for displaying single products, called single-product.php.
    2. A file for deploying all single posts of any post type, called single.php.
    3. The generic file for deploying all kinds of content, called index.php.
    When WordPress displays a product archive, it will work through these template files:
    1. A file for displaying the product archive, called archive-product.php.
    2. A generic archive template, called archive.php.
    3. The default template, index.php.
    WordPress will work through this hierarchy and use the template file it comes across first. So if you want to create a custom template for your products, a good place to start is by copying the single.php file, saving it as single-product.php and editing that.
    However if you don’t want to create template files, WordPress will happily use the files already present in your theme, which is what it’s done for me with the twenty fifteen theme’s archive.php and single.php files.

    Summary

    Being able to create custom content makes WordPress very powerful: you can uses it to add and display a range of flexible content types in whichever way you need to.
    In this post you’ve learned about the three types of custom content, what they are and when to use each of them. You’ve also learned how to create a custom post type and display it on your site.
    In the next part you’ll learn how to create custom taxonomies and custom fields.
    Do you use custom post types in your sites? Do you prefer to use a plugin or code your own? What do you find them most useful for? Let us know your thoughts in the comments.