Development in WordPress Way

Development in WordPress way

As WordPress itself have a PHP base so Sometimes our search and development flow reach us to PHP code. which is not bad at all to use PHP functions or sometimes to write core PHP code.

but we have to keep in mind there are many things already coded in WordPress. so sometimes just one WordPress function call may reduce your lines of PHP code.

Development Tips for WordPress Developer

As like other CMS platform, WordPress has already created needed function
https://codex.wordpress.org/Function_Reference/

So instead of writing custom function unless the function is already exist in WordPress code base will not be code reusablit.
So inorder to maintain code reusablity and make WordPress development easy you can use WordPress function and also edit them using actions and fiters provided by WordPress.

Following the common function that I use instead of writing PHP code.

Selected()

WordPress has a function selected() that get use to print ‘selected‘ text when two value match.
generally it is use to make dropdown and radiobox selected.

so instead of writing php code we can reduce the work with single WordPress function call.
It is not only code reuse but also easy to use.

<!-- Testing the values with if() -->
<select name="options[foo]">
    <option value="1" <?php if ( $options['foo'] == 1 ) echo 'selected="selected"'; ?>>1</option>
    <option value="2" <?php if ( $options['foo'] == 2 ) echo 'selected="selected"'; ?>>2</option>
    <option value="3" <?php if ( $options['foo'] == 3 ) echo 'selected="selected"'; ?>>3</option>
</select>
 
<!-- Using selected() instead -->
<select name="options[foo]">
    <option value="1" <?php selected( $options['foo'], 1 ); ?>>1</option>
    <option value="2" <?php selected( $options['foo'], 2 ); ?>>2</option>
    <option value="3" <?php selected( $options['foo'], 3 ); ?>>3</option>
</select>

You can drab more info about this function here

 

Checked()

WordPress has a function checked() that get use to print ‘checked‘ text when two value match.
generally it is use to make checkbox.

so instead of writing php code we can reduce the work with single WordPress function call.
It is not only code reuse but also easy to use.

<!-- Testing the values with if() -->
<input type='checkbox' name='options[postlink]' value='1' <?php if ( 1 == $options['postlink'] ) echo 'checked="checked"'; ?> />
<!-- Use Checked() instead -->
<input type="checkbox" name="options[postlink]" value="1" <?php checked( $options['postlink'], 1 ); ?> />

You can drab more info about this function here

Similarly we have other function listed below those can be relacement of your lines of PHP code.

Conclusion

I was also not aware of these common use function of WordPress.
I encountered them in Code review at my first organization.
I personally think Code review is really important to learn new things or correct old mistakes.  I consider it more than code correction i.e knowledge sharing.

If this may possible then you can suggest your friend co-worker to be your peer reviewer. you could review his code to share knowledge at the same time. if you feel someone is more knowledgeful developer, then don’t ever miss the chance to get your code reviewed by that person.

you can learn a lot from this process ….

If you like my article or feel it helps you, Let me know in the Comment section below… Thank you 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *