How to write dynamic css in a php file in wordpress

It’s always standard to maintain a separate css file for styling, even for dynamic css that is generated by the theme options. Most of the theme developers include this dynamic styles into the header or footer using wp_head or wp_footer hook. How’s about having a totally separate css file for the dynamic styles? :) It’s pretty simple so let’s start!

In my example, I have used redux framework, so that I have a global variable. In my case, I assume my global variable is $lts. So, in functions.php you need to add like this:

[php]
// Adding custom script
add_action( ‘wp_enqueue_scripts’, ‘theme_custom_style_script’, 11 );
function theme_custom_style_script() {
wp_enqueue_style( ‘dynamic-css’, admin_url(‘admin-ajax.php’).’?action=dynamic_css’, ”, VERSION);
}

// Ajax handler for wordpress
add_action(‘wp_ajax_dynamic_css’, ‘dynamic_css’);
add_action(‘wp_ajax_nopriv_dynamic_css’, ‘dynamic_css’);
function dynamic_css() {
// Assuming the css file is in /wp-content/themes/THEME_NAME/assets/css/ directory
require( get_template_directory().’/assets/css/custom.css.php’ );
exit;
}
[/php]

Then in custom.css.php file add this:
[php]

body{
/* You can use your theme option variable here, just declare it as global variable first */
color: ;
}
[/php]

Gist code:

https://gist.github.com/bappi-d-great/208d06bc3139f2076074

Hope it helps! :)

How to load a wordpress plugin at very last

You need to load your plugin at vert last? Well, sometimes we develop a simple plugin that is dependent to other plugins, something like add-on. So, in those cases, the addon plugins need to be loaded after the parent plugin. Here is a small snippet that need to put at the addon plugin to make sure that the plugin will be load at the last of all plugins.

Here you go:
https://gist.github.com/bappi-d-great/26808240df88dd1fc3fe

Enjoy!

How to make your wordpress secure, wp config tips and many more

You don’t want your site to be hacked, right? Security should be at the top of to-do list. It doesn’t matter which CMS or platform you use for your website or application or portal, but you need to always think about security. Well, you never can stop a hacker to hack your site, but you can make this difficult for you. Today we are going to discuss about some security issue and wp-config tips.

wp-config.php file is the key of a wordpress site, like a foundation when you build a building. Everything in wordpress stand based on this configuration file. So, it’s important, but most of the users ignore this file or afraid of even looking at this file. But you know what, once you get familiar with this your life would be easier :)

So, let’s start!

  1. Make sure the file permission wp-config.php is 600 to prevent other users on the server from reading it. The permission of other directories should be 750 or 755. And all files should be 640 or 644. Never use 755 for a file or directory, not even for upload folder!
  2. To check any error in your site, you can enable debug mode. Though, enabling debug mode in a live site is not recommended at all. You should have a staging site where you can do all the testing, and when you are done, apply the changes in production server. Anyway, to enable debug more edit a line in wp-config.php. Change

    to

    It will enable debug mode and you will see all the warnings, errors and notices (based on server configuration).

    Now, if you want to enable debug mode but don’t want to display the errors, then use:
    [php]
    define( ‘WP_DEBUG_DISPLAY’, false );
    @ini_set(‘display_errors’,0);
    [/php]
    So, how can you see the errors? Well, you can enable logging the errors :) Adding the following line will create a debug.log file inside of wp-content directory with all the errors and notices.
    [php]
    define( ‘WP_DEBUG_LOG’, true );
    [/php]

  3. So, as you saw the define function, so I hope you are familiar with it or will be :) You can take some advantage of this function. This function is used to define a constant. A constant is a variable but the value of that variable never be changed and you can use that constant anywhere in your site (something like global variable, but you don’t declare it globally before using). Example:
  4. Did you install SSL in your server for your domain? And the site admin is still being loaded over HTTP instead of HTTPS? Well, you can force SSL login.
    [php]
    define( ‘FORCE_SSL_LOGIN’, true );
    [/php]
    You can force the admin of your site to be loaded over HTTPS as well:
    [php]
    define( ‘FORCE_SSL_ADMIN’, true );
    [/php]
    If you use non secure virtual host, you can add this in httpd.conf file: (assuming your site is domain.com)
    [html]
    RewriteCond %{THE_REQUEST} ^[A-Z]{3,9} /(.*) HTTP/ [NC]
    RewriteCond %{HTTPS} !=on [NC]
    RewriteRule ^/?(wp-admin/|wp-login.php) https://domain.com%{REQUEST_URI}%{QUERY_STRING} [R=301,QSA,L]
    [/html]
  5. How’s about disabling theme and plugin editor? Editing via wordpress editor is really a bad habit unless you are sure what you are doing as there is no way to roll back. Adding the following line will disable the editors:
    [php]
    define( ‘DISALLOW_FILE_EDIT’, true );
    [/php]
    If you want to restrict updating themes and plugins from inside the wordpress admin use:
    [php]
    define( ‘DISALLOW_FILE_MODS’, true );
    [/php]
  6. Revision is one of the coolest features in wordpress. Besides it also increases the db size. So you can limit the revisions using the following code to 5 times:
    [php]
    define( ‘WP_POST_REVISIONS’, 5 );
    [/php]
    If you want to completely disable the revision feature use:
    [php]
    define( ‘WP_POST_REVISIONS’, false );
    [/php]
    Plus, if you want to increase the delay (in this example 1500s) of autosave, use this:
    [php]
    define( ‘AUTOSAVE_INTERVAL’, 90000 );
    [/php]
  7. In latest version of wordpress the default theme is twentyfourteen. You can change the default theme to any installed theme. You must need to know the theme slug.
    [php]
    define( ‘WP_DEFAULT_THEME’, ‘twentytwelve’ );
    [/php]

Include GitHub Gists in WordPress content

WordPress.com makes it dead easy to include Gists in posts, but that isn’t available in the WordPress software by default.  So let’s add it!

GitHub’s Gist service normally provides an embed code to include Gists in other web sites. The embed code is really just a script to load the Gist via JavaScript. Since WordPress normally strips content like that (for security purposes), we’ll use a little shortcode snippet to make it easy to reference Gist code.

Here’s that shortcode snippet:

https://gist.github.com/Ugotsta/49d45a54022f00060f71

That snippet can be added to your theme’s functions.php, via a custom plugin or using Code Snippets.

Once added, you’ll be able to add Gist references by url like so:
[gist url="https://gist.github.com/Ugotsta/49d45a54022f00060f71"]

Please note, we cannot be held accountable if you choose to change that url reference and it blows up your website. ;)

Limit one blog per user in a wordpress multisite

In a wordpress multisite, an user can create as many blog as he wants. If you Pro Sites you can control this. Pro Sites is a giant plugin that will help you to have control over the whole network.

But if you just want to limit one blog per user in a wordpress multisite and don’t want to use a huge plugin like Pro Sites, then this small snippet should help you.

https://gist.github.com/bappi-d-great/7b3828009bfd7015b526

You can add those codes in your functions.php in the theme, if you think your theme won’t be changed. Otherwise mu-plugins is the best solution. To use mu-plugins, go to /wp-content/ and find the folder with name ‘mu-plugins’. If there is no folder in that name, then create a folder, name it ‘mu-plugins’, create a file inside that, give any name you like and paste the code in there. You don’t need to activate that plugin. Mu-plugins means must use plugins, so it will be activated automatically always. If you use mu-plugins then add a php start tag at the beginning of the code.

Enjoy!

Easily display member cards with WPMU DEV Membership

Here’s a simple shortcode snippet to let you easily add a membership card for your site’s members when using WPMU DEV’s Membership plugin. This was requested in the WPMU DEV forums, where we love to help members out with simple and useful custom snippets. :)

WPMU DEV Forum topic: Display Membership Data Print/Mobile

The snippet adds a [mem_card] shortcode that will then show some member details.
https://gist.github.com/bappi-d-great/5ab5a1bd9684d8512b39

The snippet can easily be added to your theme’s functions.php file, using the downloadable file available in that forum thread or using a plugin like Code Snippets.

How to add multiple custom excerpt length in wordpress

In archive pages, we use the_excerpt() function to show excerpt of each posts. Be default, wordpress uses 55 words as excerpt if you don’t enter anything in Excerpt meta field. Now, if you want to change the length of excerpt, there is a filter available for that. excerpt_length is the filter that you can use to customize the excerpt length, just use it following way:

[php]

function new_excerpt_length($length) {
return 20;
}
add_filter(‘excerpt_length’, ‘new_excerpt_length’);
[/php]

You can change 20 to some other integer.

But what if you need to use multiple length of excerpt in different archive pages? We, in that case I prefer to use a custom excerpt function. That will give the output exactly same as the_excerpt() function. Just, you need to call a custom function that we will write now and pass the length as a parameter. Here is the function:

[php]
function custom_excerpt($charlength = 55) {
$res = ‘

‘;
$excerpt = get_the_excerpt();
$charlength++;
if(strlen($excerpt) > $charlength) {
$subex = substr($excerpt, 0, $charlength-5);
$exwords = explode(” “, $subex);
$excut = -(strlen($exwords[count($exwords) – 1]));
if($excut < 0) {
$res .= substr($subex, 0, $excut);
} else {
$res .= $subex;
}
$res .= "[…]";
} else {
$res .= $excerpt;
}
$res .= '

‘;
echo $res;
}
[/php]

What you have to do is to call custom_excerpt() function with a length like custom_excerpt(40). In my function, if you don’t pass any parameter then the default value is set to 55. You can change with your own.

You can add those codes in your functions.php in the theme, if you think your theme won’t be changed. Otherwise mu-plugins is the best solution. To use mu-plugins, go to /wp-content/ and find the folder with name ‘mu-plugins’. If there is no folder in that name, then create a folder, name it ‘mu-plugins’, create a file inside that, give any name you like and paste the code in there. You don’t need to activate that plugin. Mu-plugins means must use plugins, so it will be activated automatically always. If you use mu-plugins then add a php start tag at the beginning of the code.

Make sure you use the function within a loop.

WordPress: How to load all styles and scripts in footer

We want to load styles and scripts in footer in wordpress to load it faster. In fact the best practice is to load styles in head and the scripts in footer. We can use some minify plugins to minify all styles and/or scripts. But sometimes it creates issues, in most cases in SSL enabled pages minified styles do no work.

Also, we don’t have control in third party plugins, some plugins pushes scripts in head and some pushes in footer. So, it’s not possible to go through all plugins and make changes.

I have a little code snippet that will force to load all styles and scripts into the footer. Basically the logic is to remove action from head and adding it back to the footer.

Here you go:
[php]
remove_action(‘wp_head’, ‘wp_print_scripts’);
remove_action(‘wp_head’, ‘wp_print_head_scripts’, 9);
remove_action(‘wp_head’, ‘wp_enqueue_scripts’, 1);
add_action(‘wp_footer’, ‘wp_print_scripts’, 5);
add_action(‘wp_footer’, ‘wp_enqueue_scripts’, 5);
add_action(‘wp_footer’, ‘wp_print_head_scripts’, 5);
[/php]

You can add those codes in your functions.php in the theme, if you think your theme won’t be changed. Otherwise mu-plugins is the best solution. To use mu-plugins, go to /wp-content/ and find the folder with name ‘mu-plugins’. If there is no folder in that name, then create a folder, name it ‘mu-plugins’, create a file inside that, give any name you like and paste the code in there. You don’t need to activate that plugin. Mu-plugins means must use plugins, so it will be activated automatically always. If you use mu-plugins then add a php start tag at the beginning of the code.

Please note that, this is still dangerous, it could break your site but in most cases it should work. You can give it a go and check :)

How to enable php mail() in your ubuntu

In my last post I showed you how you can set up an ubuntu server for apache, php, mysql, phpmyadmin and wordpress multiste. Below is the link if you want to take a look:

How to setup a VPS (Ubuntu) for WordPress hosting – A to Z tutorial

Today, we will learn how we can enable email in that server, so that we can use php mail() to send mail from our system. Today I won’t go in that detail but just to set a simple mail server.

The simplest way to enable mail in your ubuntu is to run the following command:

[html]
sudo apt-get install sendmail
[/html]

It will install a copy of sendmail that listens locally. If you install that, then php can use the built in mail() function. There also won’t be a risk of becoming a remote relay if you install like this as well. If you plan on sending mail out to other internet domains, you will need to make sure the IP address of the machine sending mail has a reverse DNS entry (PTR record) set up as well. Most large ISP’s will reject your mail if your IP does not have a PTR record or one that points to a generic host (e.g. 1.2.3.3.domain.isp.com). It should reverse to something like mail.yourdomain.com

In other case, also I prefer to use postfix. It is also easy to install. To install run this command:

[html]
sudo apt-get install postfix
[/html]

During the installation a dialogue box will be appeared where you need to select “Internet Site” and the next box give your domain name as system domain.

Now to make it fully functional, we need to edit configuration file. Open the configuration file:

[html]
sudo nano /etc/postfix/main.cf
[/html]

Make the following changes:

[html]
myhostname = yourdomain.com
virtual_alias_maps = hash:/etc/postfix/virtual (you need to replace the line which has alias_maps)
mydestination = localdomain, localhost, localhost.localdomain, localhost, yourdomain.com
relayhost = (can be blank by default)
mynetworks = 127.0.0.0/8 [::ffff:127.0.0.0]/104 [::1]/128
mailbox_size_limit = 0
recipient_delimiter = +
inet_interfaces = all
[/html]
Press ‘cntrl x’ to exit the file and then ‘Y’ to save the file.

Reload the configuration:

[html]
sudo /etc/init.d/postfix reload
[/html]

Also, you can add users to the alias file. Open up the the alias database:

[html]
sudo nano /etc/postfix/virtual
[/html]

Enter the names and emails of the user like:

[html]
system@domain.com user1
support@example.com user2
[/html]
Press ‘cntrl x’ to exit the file and then ‘Y’ to save the file.

Once you are finished, save, exit, and run the following command to enable:

[html]
postmap /etc/postfix/virtual
[/html]

Again restart the postfix:

[html]
sudo /etc/init.d/postfix reload
[/html]

Now we need to test. Create a file in your server root, called – mail.php, for example. Add the following code:

[php]
<?php
$headers = ‘From: yourmail@gmail.com’ . “rn” .
‘Reply-To: yourmail@gmail.com’ . “rn” .
‘X-Mailer: PHP/’ . phpversion();
if(mail(“targetMail@gmail.com”, “Test Email”, “Email is set up”, $headers)) echo “Correct”;
else echo “Wrong”;
[/php]

And run the file by http://yourdomain.com/mail.php

If it prints “Correct”, then email is setup and check the inbox :)

You also can try sending email from your terminal. Run the following command:

[html]
sendmail targetMail@domain.com
[/html]

Then write the email contents and press Ctrl + D to send email.

Now, play with emails in your server :)

How to hide a plugin for non-admin users in wordpress

Today I will show how you can hide a plugin from the users that are non-admin. If you use a multisite, you can control a lots of stuffs with Pro Sites plugin. But what if you don’t want to use Pro Sites?

This simple scripts will do the job for you.

[php]

function custom_plugins( $plugins ){

if ( !is_super_admin() ) {
// Add comma separated paths of plugins that you want to hide
$hide_plugins = array(‘pluginFolder/pluginFile.php’, ‘pluginFile.php’);
foreach ($hide_plugins as $hide_plugin) {
unset($plugins[$hide_plugin]);
}
}
return $plugins;
}
add_filter(‘all_plugins’, ‘custom_plugins’);
[/php]

You can add those codes in your functions.php in the theme, if you think your theme won’t be changed. Otherwise mu-plugins is the best solution. To use mu-plugins, go to /wp-content/ and find the folder with name ‘mu-plugins’. If there is no folder in that name, then create a folder, name it ‘mu-plugins’, create a file inside that, give any name you like and paste the code in there. You don’t need to activate that plugin. Mu-plugins means must use plugins, so it will be activated automatically always. If you use mu-plugins then add a php start tag <?php at the beginning of the code.

I have shown option for both – 1. if the plugin file is inside a folder 2. if the plugin file is in plugin root folder.

Just add more items in the array to hide :)