Removing WordPress menu div tag and keeping ul [Code Snippet]

When you are working with WordPress menu then sometimes you may noticed if no menu is defined it is putting a div wrapper just above the url tag. Here is how you can remove that div and keep ul as it as.

wp_nav_menu( 
    array( 
        'theme_location'  => '', //Just keep this as empty
        'container'       => '', //Keep this one also as empty
        'menu_class'      => 'menu',
        'menu_id'         => 'primary-menu',
        'depth' => '2',
    )
);

Checking if a menu published in WordPress

$has_enabled_main_menu = false; 
$menu_location = 'primary'; //Going to check if this menu location (primary) has a menu as defined
$menu_locations = get_nav_menu_locations();
$menu_object = ( isset( $menu_locations[ $menu_location ] ) ? wp_get_nav_menu_object( $menu_locations[ $menu_location ] ) : null );
if( $menu_object ) {
    $has_enabled_main_menu = true;
}

Finding all parent pages in WodPress [Code Snippet]

Sometimes we needs to find all parent pages of current page in WordPress. It is useful to create a breadcrumb. Here is a example code snippet to find all parent pages.

<ul class="page-breadcrumbs">
    <li><a href="<?php echo home_url(); ?>"><?php _e( 'Home', 'mondira' ) ?></a></li>
    <?php
    global $post;
    $parent_id = $post->post_parent;
    $breadcrumbs = array();
    while ( $parent_id ) {
        $page = get_page( $parent_id );
        $breadcrumbs[] = '<li><a href="' . get_permalink( $page->ID ) . '">' . get_the_title( $page->ID ) . '</a></li>';
        $parent_id = $page->post_parent;
    }
    $breadcrumbs = array_reverse( $breadcrumbs );
    foreach ( $breadcrumbs as $crumb ) echo $crumb;
    ?>
    <li><a href="<?php the_permalink(); ?>"><?php the_title();?></a></li>
</ul>

WordPress sidebars fetching on other cms with wordpress xml rpc

Recently I worked on a site which is using wordpress as it’s cms and creloaded as it’s web shop. So the wordpress was top of the shopping cart and then the shopping cart as webshop. So basically I had to develop the site using wordpress and then had to install that creloaded inside woordpress installation.

For main site wordpress has it’s header (with navigation), footer (with navigation and other contents), sidebar with widgets etc. So when I am visiting the web shop with creloaded I was required to show the same layouts and design. For that I had to use wp xmlrpc to fetch those common features into creloaded system.

XML-RPC functionality is turned on by default since WordPress 3.5. So anyone can use it who is using wp 3.5 or greater without activating xml-rpc via wordpress admin!

All of this can be done with below written 3 steps.
1. Writing XML Client Class
2. Using XML Client
3. Defining XML Server methods in wp functions.php

Note: Here all the things are not well documented, Because I am assuming you are enough expert on wp and did some google and finally here to see a code example of wp xml rpc implementations.
Read More

z-index of dropdown menu vs iframe (youtube) video issue

When you are using iframe to display any video, for example if you are using youtube iframe video and on your site it has a dropdown menu and your dropdown memu is showing behind the iframe video you can make it top of iframe video with below written jquery code.

(function ($) {
    $ = jQuery;
    $(function () {
        $video = $("#parentocontainer> iframe");
        $srcVal = $video.attr('src');
        appendedVal = $srcVal + "?wmode=opaque";
        //or
        //appendedVal = $srcVal + "&wmode=opaque";
        $video.attr('src',appendedVal);
    });
 })(jQuery);

Contact form 7 nice css3 styles

If you are using wordpress and for contact form Contact us 7 plugins then you can use below css3 codes to make that contact form 7 css looks nicer then default one!

/*Contact Form 7 CSS Support*/
span.wpcf7-not-valid-tip{display: none !important; }
div.wpcf7 .wpcf7-not-valid{ border: 1px solid #F00; margin: 0; }
div.wpcf7-validation-errors{ -webkit-box-sizing: border-box; /* Safari/Chrome, other WebKit */ -moz-box-sizing: border-box;    /* Firefox, other Gecko */ box-sizing: border-box;         /* Opera/IE 8+ */ margin: 0; padding: 10px; color: #c4690e; background: #fffdf3; text-align: center; border: 1px solid #e6bf4a; border-radius: 3px; -moz-border-radius: 3px; -webkit-border-radius: 3px; font-size: 12px;}
div.wpcf7 .wpcf7-not-valid { box-shadow: 0 0 6px rgba(255,0,0,0.4); } 
.your-message textarea{ width:100%; -webkit-box-sizing: border-box; /* Safari/Chrome, other WebKit */ -moz-box-sizing: border-box;    /* Firefox, other Gecko */ box-sizing: border-box;         /* Opera/IE 8+ */ }
div.wpcf7-mail-sent-ok {  -webkit-box-sizing: border-box; /* Safari/Chrome, other WebKit */ -moz-box-sizing: border-box;    /* Firefox, other Gecko */ box-sizing: border-box;         /* Opera/IE 8+ */ margin: 0; color: #5f9025; background: #ebf6e0; border: 1px solid #b3dc82; padding: 10px; text-align: center;  border-radius: 3px; -moz-border-radius: 3px; -webkit-border-radius: 3px; font-size: 12px; }
div.wpcf7-mail-sent-ng, div.wpcf7-spam-blocked {  -webkit-box-sizing: border-box; /* Safari/Chrome, other WebKit */ -moz-box-sizing: border-box;    /* Firefox, other Gecko */ box-sizing: border-box;         /* Opera/IE 8+ */ margin: 0; padding: 10px; background: #ffe9e9; color: #d04544; border: 1px solid #e7a9a9; border-radius: 3px; -moz-border-radius: 3px; -webkit-border-radius: 3px; font-size: 12px; text-align: center;}
.wpcf7-form-control-wrap input[type=email]:focus, .wpcf7-form-control-wrap input[type=text]:focus, .wpcf7-form-control-wrap input[type=text]:focus, .wpcf7-form-control-wrap textarea:focus{ border:1px solid #CCC; box-shadow:none; border-color: rgba(82, 168, 236, 0.8); outline: 0;
    outline: thin dotted \9;
    /* IE6-9 */
        -webkit-box-shadow: inset 0 1px 1px rgba(0, 0, 0, 0.075), 0 0 8px rgba(82, 168, 236, 0.6);
            -moz-box-shadow: inset 0 1px 1px rgba(0, 0, 0, 0.075), 0 0 8px rgba(82, 168, 236, 0.6);
                box-shadow: inset 0 1px 1px rgba(0, 0, 0, 0.075), 0 0 8px rgba(82, 168, 236, 0.6);
}
.wpcf7-form-control-wrap input[type=email]:focus, .wpcf7-form-control-wrap input[type=text]:focus, .wpcf7-form-control-wrap textarea:focus,.wpcf7-not-valid, div.wpcf7-validation-errors, div.wpcf7-mail-sent-ok, div.wpcf7-mail-sent-ng, div.wpcf7-spam-blocked { transition : border 1000ms ease-out;  -webkit-transition : border 1000ms ease-out;  -moz-transition : border 1000ms ease-out; -o-transition : border 1000ms ease-out; }

Unique Page Sidebars Is not working

While working on wordpress to develop any website you may need to create dynamic sidebar for different pages, custom post type. You can use Unique Page Sidebar wordpress plugins from wordpress plugins repository for free.

It works for perfectly on default wordpress single page also on different archive pages.
For custom post type if you are using this plugins like you created a custom post type page template with custom query you need to make sure that you reset your query at the end of post looping.

If some how Unique Page Sidebars is not working on your custom post type template page then you need to make sure that you used wordpress reset query like below.


Replacing media urls on wordpress posts

Sometimes we work for a website with wordpress keeping the development version on sub directory. Say are working for http://www.example.com website and currently it is live and we work for it’s new version in wordpress. So we setup a new wordpress version on new folder so the current development version is http://www.example.com/new

When we are done with all development and uploaded all contents on that new setup that means all media files and contents media urls are targeting with http://www.example.com/new

So when we are going to make the site live we need to do something manually to replace media paths for contents. Or if we directly copy contents from another hosting in that case same things will happen.

For the faster replacement of each media guid, contents media url or other meta values below mentioned 3 mysql query is nice.

Note: After running below code we need to ensure that all wp-contents/uploads folders contents are available as it was on new development directory.

update wp_posts set guid = replace(guid, 'http://www.example.com/new', 'http://www.example.com')

update wp_posts set post_content = replace(post_content, 'http://www.example.com/new', 'http://www.example.com')

update wp_postmeta set meta_value = replace(meta_value, 'http://www.example.com/new', 'http://www.example.com')

update wp_options SET option_value = 'http://www.example.com' WHERE option_name = 'home' OR option_name = 'siteurl';

 

With this above queries below listed things will happen –
1. Replace all resource or post guid
2. Replace all old media src url to new src url in post contents
3. Replace all meta resource url like image, video or audio etc
4. Replace site home page and siteurl url

how to make windows form application design looks like windows 7 – Enable Visual Styles

If you see any control visual looks is not just what it was looking on interface design mode (or windows style looks) like below 2 images

looks of interface design mode
Without Enable Visual Styles or Windows Classic Looks

Then It means you are skipping a method call from System.Windows.Forms Namespaces, that isĀ EnableVisualStyles

To Enables visual styles or operating system like controls color, fonts and other visual elements for your application just use below code before application run on Main method.

Application.EnableVisualStyles();

as below

[STAThread]
static void Main()
{
     Application.EnableVisualStyles();
     Application.SetCompatibleTextRenderingDefault(false);
     Application.Run(new Form1());
}

Returning a value to parent form from sub or popup form with C Sharp .net

It is a common features you may need it several times when you are on the way of a professional C# application development.

Suppose you have a Parent form which has a Textbox control and a Button control. Now you want to click on the button Control then it will open a Sub form or a PopUp form. In which there will be another button or any other control for example DatagridView and you want to click on it and it will return a value to it’s Parent form and that value will display on Parent forms TextBox control as text property.

You can do it by below listed steps.

Step 1 (Parent Form Code):
On parent form button control click event you can call your sub form like below code to open it and get it’s returning values from it’s predefined properties and use it for your textBox control as below.

private void button1_Click(object sender, EventArgs e)
        {
            frmSubForm objSForm = new frmSbFOrm();
            if (objSForm.ShowDialog() == DialogResult.OK) 
            {
                this.txtParent.Text = objSForm.StringProperty;
            }
        }

Step 2 (Sub Form Code):
On the sub form which one is opening after the click on button control of Parent form you can write below code for any events to return the value and exit sub form.

private void button1_Click(object sender, EventArgs e)
        {
             this.StringProperty= "Sample String";
             this.DialogResult = DialogResult.OK;
        }