Autoloading in your wordpress plugin

Autoloading in your wordpress plugin

An important thing to note is that you will want your autoloader to load classes from your plugin only. If you do not enforce this restriction, your plugin may end up conflicting with WordPress or other plugins.

To enforce this restriction, simply check the classNamevariable in the autoload function for your plugin’s namespace. An example is shown below

if(strpos($className, __NAMESPACE__) !== 0)
        return;

So a simple autoloader would look like this (replace PLUGIN_PATH_HERE with your plugin’s path)

spl_autoload_register(__NAMESPACE__ . '\\autoload');
function autoload($className)
{
    $ext = '.php';
    $cls = ltrim($className, '\\');
    if(strpos($className, __NAMESPACE__) !== 0)
        return;

    $cls = str_replace(__NAMESPACE__, '', $className);
    $path = PLUGIN_PATH_HERE . str_replace('\\', DIRECTORY_SEPARATOR, $className) . $ext; // Replace PLUGIN_PATH_HERE with your plugin path
    require_once($path);
}

 

Enjoyed the content ? Share it with your friends !

WordPress the correct way to load jQuery for your plugin or theme

To load a script in wordpress, you have to use the function wp_enqueue_script

Often people call the function like this

wp_enqueue_script('my-script', get_template_directory_uri() .'/js/my-script.js');

However, you may come across dependency problems if you do it like the above. Hence, it is best practice to use the other parameters, especially the third one which specifies the dependency. For example, if your script relies on jquery,

wp_enqueue_script('my-script', get_template_directory_uri() .'/js/my-script.js', array('jquery'));

Also, you can check if jquery is already enqueued by using

if ( ! wp_script_is( 'jquery', 'enqueued' ) ) {
    wp_enqueue_script( 'jquery' );
}

Finally, note that wp_enqueue_script is for the front-end of your site. If you wish to queue scripts for the admin panel, use admin_enqueue_scripts

Enjoyed the content ? Share it with your friends !

PHP Parking Page Template Source Code

PHP Parking Page Template Source Code

<?php

$domain = ""; // You may hardcode it if you wish
if( empty($domain) ) {
    $domain = $_SERVER['HTTP_HOST'];
}
?>

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
    <meta name="apple-mobile-web-app-capable" content="yes">
    <title><?php echo $domain; ?></title>
    <style>
    * { 
      box-sizing: padding-box;
    }
    html {
      height: 100%;
    }
    body {
      font-family: "Courier New", "Courier", monospace;
      font-size: 12px;
      height: 100%;
      margin: 0;
      padding: 0;
    }
    .container {
      height: 100%;
      position: relative;
    }
    .content {
      text-align: center;
      position: relative;
      top: 50%;
      transform: translateY(-50%);
    }
    .domain {
        padding: 5px;
        font-size: 3em;
    }
    </style>
  </head>
  <body>
    <div class="container">
      <div class="content">
        <div class='domain'>
          <?php echo $domain; ?>
        </div>
        <br/>
        <div class="contact">
          <p>Interested buyers may contact me through the email shown in the image below</p>
          <p>如果您想购买这个名域,您可以通过我的电邮来联络我</p>
          <img src="email.bmp"/>
        </div>
      </div>
      
    </div>

  </body>

</html>

All you need to do is create a bitmap file that displays your email address. This is meant to obscure your email to prevent spammers from scraping your email and spamming you.

Enjoyed the content ? Share it with your friends !