Can I create my own “Recent Posts” widget or customize the existing one?

It’s usually best to copy the existing one, name it something unique, and then add your own functionality. Copy the WP_Widget_Recent_Posts class from wp-includes/class-wp-widget-recent-posts.php into your functions.php (or, preferably, another file in your theme devoted just to widgets) and rename to something else, like My_Widget_Recent_Posts Add your functionality in there. Don’t forget to call register_widget(‘My_Widget_Recent_Posts’) … Read more

wp_redirect() – headers already sent

Found the answer (via) Instead of using the function I added an action to “wp_loaded”, that makes sure that it gets loaded before any headers are sended. <?php add_action (‘wp_loaded’, ‘my_custom_redirect’); function my_custom_redirect() { if ( isset( $_POST[‘subscribe’] ) ) { $redirect=”http://example.com/redirect-example-url.html”; wp_redirect($redirect); exit; } } ?>

Solutions for generating dynamic javascript / CSS

One additional option, depending on the kind of parameters you need to pass in. Let’s call it (2a). You can also create PHP scripts which output dynamically-generated text/css or text/javascript rather than text/html, and provide them the data that they need using GET parameters rather than by loading up WordPress. Of course this only works … Read more

Theme Activate Hook

I have that code here just name the file theme_activation_hook.php like on the website and copy this. <?php /** * Provides activation/deactivation hook for wordpress theme. * * @author Krishna Kant Sharma (http://www.krishnakantsharma.com) * * Usage: * ———————————————- * Include this file in your theme code. * ———————————————- * function my_theme_activate() { * // code … Read more

get_template_part vs action hooks in themes

I prefer hooks, since they are more flexible: you can hook into them from your theme’s functions.php file, but also from plugins. I try to put as much logic in plugins, so that the themes contain mostly layout stuff. If you use an action hook, it is still possible to use get_template_part() in that hook … Read more

pass object/JSON to wp_localize_script

Indeed, wp_localize_script() is simple, it just adds quotes around the values and escapes the content, expecting all of them to be strings. However, there is the l10n_print_after key of the array, which will be printed without any interference at all. It can be used to execute arbitrary code after the strings are passed. You can … Read more