Custom code in WordPress

You know that in last years WordPress has become to be full CMS, not just blog system. It makes us happy because there are a lot of developers who love WordPress more than other CMS-es. For example me, i used to write codes only in frameworks, but i had time to taste most known CMS-es: Joomla, Drupal, Phpnuke, WordPress and etc. Each CMS has its own advantages and disadvanages and there are a lot of comparison tables in internet, you can find it and compare CMS-es easily.(for examle)
But in this article i will not compare CMS-es. I have already choosen WordPress as my only CMS, and i have own approach to work with it.
We will consider that you are a web developer, you love coding, but you don’t have enough practice in Wordpess, and in near future you aren’t going to be professional wordpress developer who should know creating&editing themes, plugins, taxonomies or must know Wordress Codex. You just want to build simple website in wordpress then to develope it with own codes-without making plugins,themes. Of course it is better to follow WordPress standarts and work with themes,plugins,taxonomies and etc. But in this post we will consider that you want to write own simple codes. If it really so, let’s begin. We will consider that we installed it to localhost for testing. For examle: 127.0.0.1/wp

Execute php codes inside posts.
Install very useful plugin – InlinePHP. Download it from WordPress and then install it through Plugins->Add new->Upload, then activate. This plugin will let you to write and execute php codes inside posts. It is not needed always, but sometimes you might need php code inside post. Your IP address is [exec] echo $_SERVER[‘REMOTE_ADDR’]; [/exec]. You see, i get your IP address inside my blog post via inline php. You should use [ exec ] //…your code here …. [ / exec ]
for executing php code inside your post.

Writing custom codes inside theme.
Go to Appearance->Editor. And begin editing yout theme.
Short about wordpress themes: WordPress theme can contain a lot of files. But main skeleton contains 4 parts: Header, body(page,loop or single), sidebar and footer. (In the next posts of this serie we will talk about custom themes, now we stay on default theme). We can do anything we want in this php files. WordPress code is very clever and it has not any conflict with custom-guest codes by coder.
I will give 2 examples which you can put into your theme and test it.

  • to connect to database then to run sql queries, fetching data, update table and etc. database operations.  anywhere and anytime
  • to pass and to receive variables(GET,POST,SESSION,REQUEST…)

With these 2 examples you will be able to work with wordpress database and to send&receive request variables inside wordpress.

Working with database:

global $wpdb;
 global $wp_query;
$query1="SELECT user_nicename as name from wp_users ";
$wpdb->query($query1);
 $ucount=$wpdb->num_rows;
echo 'We have '.$ucount.' users!';
$wpquery = $wpdb->get_results($query1);
foreach ($wpquery as $wpq)
 {
echo $wpq->name.'<br>';
 }

For more feautures you should read about wpdb class.

Using variables:
You can use session,get,post,request variables inside wordpress without any conflict with wp core, but be careful and follow your code to be secure. Because  wordpress can not protect your custom code if you use such variables.

<form action="post">
Enter your username(or another user's username) and see all comments by you(by anybody):
<input name="ulogin" type="text" />
<input type="submit" value="Let's go" />
</form>
<?php if (isset($_POST["ulogin"])) {   
  global $wpdb;
  global $wp_query;   $query1="SELECT comment_content from wp_comments where user_id=".$wpdb->escape($_POST["ulogin"]);
  $wpdb->query($query1);
  echo 'Comments:<br>'
  $wpquery = $wpdb->get_results($query1);
  foreach ($wpquery as $wpq)
     {
       echo $wpq->comment_content.'<br>';
     }
}
?>

2 comments on “Custom code in WordPress

  1. i want to start cutum in wordpress plaese send me information how do we make template file i know but i am not sure plaese update me with that information and also advise me how we start my custum …………..]

    thanks
    kushagra daharwal
    php programmer
    and magento developer

  2. I’m looking for someone who can code the search by product function in WooCommerce to allow searching by attributes which I have created. Specifically, I need to be able to search by author and ISBN. Woo has perfect them that does that coming out, but it’s not available yet. So I’m looking for someone who can do it for the right price. Any takers?

Leave a Reply to kushagraCancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.