Creating shortcodes in WordPress

Wordpress shortcode image

Creating shortcodes in important to any wordpress plugin developer, it’s so powerful when you can master it .

I will demonstrate in the following example the way you can use it in your project


function func_name() {
  echo "hello world";
) );

}
add_shortcode( 'shortcodeid','func_name' );

This the simplest example, here we made a function called func_name in this function we just echoing hello world .

You can try it in your post like the following [shortcodeid], that is OK but we want to add more parameters to the shortcode, let’s do that .

function func_name( $name , $another_name) {
  echo "hello world $name and $nother_name";
) );

}
add_shortcode( 'shortcodeid','func_name' );

As you saw above, we passed two parameters to the function $name and $another_name and also can be called like the following [shortcodeid name=”jon”  another_name=doe].

Note here : you can remove the double parenthesis incase it’s only one word not two separate words .

The only problem here that when the user enter the shortcode without the parameters it will trigger an error , so we have to reformat our code more to like the following .


function func_name( $atts ) {
  $atts = extract( shortcode_atts( array( 'first_name'=>'joe', 'second_name' => 'doe' ),$atts ) );

echo "hello world $first_name and $second_name";
}
add_shortcode( 'shortcodeid','func_name' );

You can find here the new shortcode_atts and it’s used for assigning a default value for the parameters, also notice that we used here extract function and it’s used to assign the keys of the array to the a variable with the same name

Make WordPress dashboard language different than frontend language

If you was in position that you wanted to change the language of the dashboard ( admin area ) than the front end area, here what you can do .

  1. You have to get a translation for your theme, install the theme in your favorite language of your choice ( in my case it was Arabic ) then copy the language files in the following directory wp-content/languages/
  2. Copy the translation to this directory ( or create it if it’s doesn’t exist ), then change the default language in wp-config.php
  3. Open your function.php file and put the following code in it
  4. add_filter( 'locale', 'set_my_locale' );
    function set_my_locale( $lang ) {
    if ( !is_admin() ) {
    
    return 'ar';    // or any other language
    
    } else {
    
    return $lang;   // Default lang
    
    }
    }
    
    

You can find an example from wordpress codex in the following link .

Wpml problem with the slides ?

there is a problem that you may face when working with WPML plugin for translating the content for your website
in some cases the slides ( custom post type ) translated doesn’t show and only show the default language
in this case you have to enable ( show translated posts only ) from WPML .

WPML >> Blog posts to display >> Only translated post ( Check it ) 

6-18-2013 11-31-32 AM

 

 

 

 

reference : http://wpml.org/forums/topic/home-page-shows-custom-posts-in-all-languages/

 

WordPress links redirect to the spam website

Once i opened the website and i found that all links on the website tend redirect to another spam website

After some searching i found the problem with the .htaccess file and it needed to be edited ( i think that the .htaccess file taken 777 ) so it can be changed by any user

Check the following links for more information :

http://wordpress.org/support/topic/website-redirect-to-another-site-containing-malware

Making contact form 7 open in new windows on submission ?

The best way to make CF7 open in new page on submission, you can you the following code by adding it at the additional setting section in the contact ( you will find it at the bottom of the contact form )


on_sent_ok: "location.replace('http://website.com');"

References :
http://contactform7.com/redirecting-to-another-url-after-submissions/

http://wordpress.org/support/topic/plugin-contact-form-7-redirect-to-thank-you-page-on-submit?replies=23