How to use internal css in WordPress

One of the way to load CSS in your WordPress plugin. You no need to worry about caching while developing your code and no need to separate one more file.

function my_style_inline() {
  $content = file_get_contents(ABSPATH . 'wp-content/plugins/PLUGINNAME/CSSFILENAME.css');
  echo '<style type="text/css">' . "\n";
  echo $content;
  echo "</style>\n";
}
add_action('wp_head', 'my_style_inline');

How To Get Parent Category Name In WordPress

It is not difficult to get the name using the build in function from WordPress. You may use this for build your own plugin or list out the related post using the category data from the parent category.

Here is the PHP Code that can display out the name of the parent category.

<?php
$category = get_the_category();
$parent = get_cat_name($category[0]->category_parent);
if (!empty($parent)) {
echo $parent;
} else {
echo $category[0]->cat_name;
}
?>

How to Solve WordPress asked for FTP credentials

I know it is very annoying when you are going to install a PLUGIN in WordPress. I like the way, one click and install for PLUGINS and THEMES in WordPress. Here is the way that I’m using:

Edit your wp-config.php and add the line into it.

define('FS_METHOD','direct');

How to disable AutoSave in WordPress ?

Sometimes we do not need the auto save function in the editor in WordPress, it will increase your database or slow down you editing experience while you are in slow internet connection.

Here is the solution to disable it, if you need to re-enable it just comment them away.

define( 'AUTOSAVE_INTERVAL', 60*60*60*24*365 ); // Set autosave interval to a year
define( 'EMPTY_TRASH_DAYS',  0 ); // Empty trash now: Zero days
define( 'WP_POST_REVISIONS', false ); // Do not save any revisions

Publishing Failed in WordPress

When you post in WordPress with over 5MB text post, you may receive this error with red color.

Publishing Failed

or you found in your php error log

PHP Fatal error:  Maximum execution time of 60 seconds exceeded in /home/nginx/domains/juzhax.com/public/wp-includes/formatting.php on line 2295

This is because WordPress need more time to execute and process and formatting your post over the maximum execution time.

You may use the way below to solve this.

Edit the file wp-config.php and add in the line


@ini_set('max_execution_time', 1800);

But, use this with caution, because if many of your script in WordPress over this time, this way maybe kill your server.

Create WordPress widget template from scratch

I’ve save down the most basic widget code for WordPress, you can copy this to your plugin and modify it to start your first widget. You can save time on this tutorial to help you create wordpress widget from scratch.

[code lang=”php”]
<?php

class example_widget extends WP_Widget {
function __construct() {
parent::__construct(false, $name = ‘Example Widget’);
}

/** @see WP_Widget::widget — do not rename this */
function widget($args, $instance) {
extract( $args );
$title = apply_filters(‘widget_title’, $instance[‘title’]);
$message = $instance[‘message’];
?>
<?php echo $before_widget; ?>
<?php if ( $title )
echo $before_title . $title . $after_title; ?>
<ul>
<li><?php echo $message; ?></li>
</ul>
<?php echo $after_widget; ?>
<?php
}

/** @see WP_Widget::update — do not rename this */
function update($new_instance, $old_instance) {
$instance = $old_instance;
$instance[‘title’] = strip_tags($new_instance[‘title’]);
$instance[‘message’] = strip_tags($new_instance[‘message’]);
return $instance;
}

/** @see WP_Widget::form — do not rename this */
function form($instance) {
$title = esc_attr($instance[‘title’]);
$message = esc_attr($instance[‘message’]);
?>
<p>
<label for="<?php echo $this->get_field_id(‘title’); ?>"><?php _e(‘Title:’); ?></label>
<input class="" id="<?php echo $this->get_field_id(‘title’); ?>" name="<?php echo $this->get_field_name(‘title’); ?>" type="text" value="<?php echo $title; ?>" />
</p>
<p>
<label for="<?php echo $this->get_field_id(‘message’); ?>"><?php _e(‘Simple Message’); ?></label>
<input class="" id="<?php echo $this->get_field_id(‘message’); ?>" name="<?php echo $this->get_field_name(‘message’); ?>" type="text" value="<?php echo $message; ?>" />
</p>
<?php
}

} // end class example_widget
add_action(‘widgets_init’, create_function(”, ‘return register_widget("example_widget");’));
?>
[/code]

lazy-result.js ReferenceError: Promise is not defined

[code lang=”javascript”]
node_modules/postcss/lib/lazy-result.js:157
this.processing = new Promise(function (resolve, reject) {
^
ReferenceError: Promise is not defined
[/code]

When I install the FoundationPress, after I run gulp build I saw this message,
this is because the node version problem.

the have mention in this post
https://github.com/postcss/postcss-nested/issues/30

Solution

[code lang=”shell”]
vim node_modules/postcss/lib/lazy-result.js
[/code]
Put this to the first line of the file lazy-result.js
[code lang=”shell”]
require(‘es6-promise’).polyfill();
[/code]
Save.

Then install
[code lang=”shell”]
npm install es6-promise
[/code]

Then build again
[code lang=”shell”]

[[email protected]]# gulp build
[18:14:07] Starting ‘clean’…
[18:14:07] Starting ‘clean:javascript’…
[18:14:07] Starting ‘clean:css’…
[18:14:07] Finished ‘clean:javascript’ after 4.56 ms
[18:14:07] Finished ‘clean:css’ after 2.98 ms
[18:14:07] Finished ‘clean’ after 6.38 ms
[18:14:07] Starting ‘build’…
[18:14:07] Starting ‘copy’…
[18:14:07] Finished ‘copy’ after 103 ms
[18:14:07] Starting ‘sass’…
[18:14:08] Starting ‘javascript’…
[18:14:08] Starting ‘lint’…
[18:14:10] Finished ‘lint’ after 1.53 s
[18:14:10] Finished ‘sass’ after 2.58 s
[18:14:14] Finished ‘javascript’ after 6.12 s
[18:14:14] Finished ‘build’ after 6.78 s
[/code]

Success !

Connect to Jetpack using behind varnish and nginx

Normally if you use varnish or other reverse proxy, you may use other port in wordpress. So you are not able to connect to Jetpack, because to connect Jetpack, you must have port 80 to authorize.

It may display the error like this from your Jetpack debugger
[code]
SELF:
Array
(
[headers] => Array
(
[server] => nginx
[date] => Sat, 23 Aug 2014 00:57:12 GMT
[content-type] => text/plain;charset=utf-8
[connection] => close
[x-pingback] => https://jetpack.wordpress.com/xmlrpc.php
[expires] => Wed, 11 Jan 1984 05:00:00 GMT
[cache-control] => no-cache, must-revalidate, max-age=60
[pragma] => no-cache
[x-hacker] => Jetpack Test
)

[body] => {"error":"Communication error","error_description":"We were unable to make an XML-RPC request to your website. Please make sure that XML-RPC is turned on and that Jetpack is installed, activated and connected with your WordPress.com account. If the error persists, try disconnecting and connecting Jetpack again."}
[response] => Array
(
[code] => 400
[message] => Bad Request
)

[cookies] => Array
(
)

[filename] =>
)
[/code]

because of the $_SERVER[‘SERVER_PORT’] is not equal to value 80;
But we can cheat the Jetpack by add this to wp-config.php
[php]
$_SERVER[‘SERVER_PORT’] = 80;
[/php]

Delete expired _transient in wordpress to optimize the database

If you separate your database to another server, and if the wp_options table is going bigger to 2MB, then it will slow down your query time, because WordPress like to query all the data from the table wp_options always.

You have to make the table smaller size to speed up the query.

I always clean up those useless _transient from the database, it could save you mostly 1MB
[code lang=”sql”]
DELETE FROM `wp_options` WHERE `option_name` LIKE (‘%\_transient\_%’)
[/code]