JS variable loaded using wp_localize_script is no longer available

TL;DR: Call the wp_localize_script after registering/enqueuing the script you are localizing.

Recently (WP 4.1.4 / WP 4.2) my ajax scripts stopped working. I’ve noticed that it was caused by the fact that the variable for the ajax_url was undefined so for some reason it ws no longer loaded to the page.

The reason turned out to be that I was using wp_localize_script to load the ajax url variable for a script that was not yet registered and enqueued.

Seeing the codex now I noticed that they have added: “IMPORTANT! wp_localize_script() MUST be called after the script it’s being attached to has been registered using wp_register_script() or wp_enqueue_script()”. However I don’t remember this not working before and also it did work until now.

So mind to check every site you have made using WP ajax and following the good practices (to load the ajax url using wp_localize_scripts) if they are still working.

Retrieving data from a form created with Contact Form 7

If you want to get the data from a form created with Contact Form 7 you can use the ‘wpcf7_before_send_mail’ hook. In your functions.php or from your plugin add action like follows:

add_action( 'wpcf7_before_send_mail', 'my_plugin_wpcf7_before_send_mail' );

function my_plugin_wpcf7_before_send_mail ( $contact_form ) {
    // TODO: get the data
}

Since version 3.9 Contact Form 7 removed $contact_form->posted data so this hook might seem like it is no longer working.

However we can still get the data but using a bit different approach provided by the Contact Form 7 API

function my_plugin_wpcf7_before_send_mail ( $contact_form ) {
    $wpcf7_submission = WPCF7_Submission::get_instance();

    $posted_data = $wpcf7_submission->get_posted_data();

    // in $posted_data you have the things sent via the form
    // try $posted_data[ 'your-email' ] to get the email from a default contact form
}

Using the $posted_data retrieved in the shown way we can get any of the fields that were sent by the user using the created form.

geeneric.com is live

We at Shtrak have been working lately on a WordPress and WooCommerce based platform for online shops.
You can create your eCommerce site for free on geeneric.com

Let me know what do you think about our service – I’m open to any ideas for improvement!

[Tips and Tricks for Writing WordPress Themes and Plugins] Forcing the browser to refresh your scripts and styles

I’m starting a some kind of thread of posts where I write down some of the nice tricks I meet in WordPress development.

First one is how to write your custom theme and plugin files so after each deployment / update you’ll be sure that the browser doesn’t use the cached version of your JS and CSS.

This one is really easy – first you define a constant in your code for the version of your theme / plugin:

// You can either define it manually
if ( ! defined( 'MY_THEME_VERSION' ) ) {
    define( 'MY_THEME_VERSION', '0.0.1' );
}
// Or you can use the version from your style.css for the theme
$theme_data = wp_get_theme();
// $theme_data [ 'Version ' ] is the theme version

// Or your version from your plugin.php for plugin with:
$plugin_data = get_plugin_data( $plugin_file );
// $plugin_data[ 'Version' ] is the plugin version

Than the only thing you need to do is to add this version number to the scripts and styles you include (that are custom for the theme).

// For CSS
wp_enqueue_style( 'my_theme_css', get_stylesheet_directory_uri() . '/library/css/style.css', array(), MY_THEME_VERSION );
// For JS
wp_enqueue_script( 'my_theme_js', get_stylesheet_directory_uri() . '/library/js/script.js', array(), MY_THEME_VERSION );

And every time you deploy you can either manually increase the version number or you can use a build script to do it for you 😉

Cheers and follow me on twitter- @ninarski

Google analytics is no loger working with WooCommerce out of the box

Recently I updated WooCommerce site from version 2.0.* to 2.1.* and today I noticed that the site stats from Google Analytics are no longer working.

I went to the settigns page and noticed that the Integration tab where the google analytics ID was suposed to be was gone.

Turns out that they dropped the Google integration out of the box in the new release and decided to put it as a separate plugin. The stupid thing is that it was not mentioned in the change log for the WooCommerce itself.

I’m getting a bit disapointed with the way WooThemes handle the most popular eCommerce WordPress plugin. Looks like not very professional approach.

Regenerating WooCommerce System Pages (When you forgot to add them)

Update: recently I found the tools tab of WooCommerce http://example.com/wp-admin/admin.php?page=wc-status&tab=tools. (It’s located in the WooCommerce -> System Status, in the Tools Tab). From there you can create your WooCommerce special pages and many more nice things.

 

When you install WooCommerce it asks you if you want to add the system pages – Shop, Checkout and so on. Unfortunately sometines you say no or you add them before you’ve changed the language so you end up either without any system pages being added or with the pages being added in the incorrect language (English in most cases 😉 ).

Good for us that there is a way to make WooCommerce regenerate the system pages. The only required thing is that you shouldn’t have the pages already added. (If you do, you should remove them completely – moving to trash only without permanently deleting them won’t work).

After you’re done with the cleaning you should add &install_woocommerce_pages=true to the settings admin url of WooCommerce.

If your site’s domain is

http://shtrak.eu

The WooCommerce settings page will be

http://shtrak.eu/wp-admin/admin.php?page=woocommerce_settings

And if you want to regenerate the default pages you should open the following url of your site:

http://shtrak.eu/wp-admin/admin.php?page=woocommerce_settings&&install_woocommerce_pages=true

And you was lucky and saved yourself some wp page addition bruteforcing 🙂

Cheers!

Error 500 on a WordPress after an intensive hacker attack

Last couple of days we’ve had a website that is actively under DDoS attack. At some point I’ve noticed that the attack have stopped (or at least my notifications for it from the security plugin we use – Better WP Security)

Opening the website I’ve noticed that it returned Error 500 so I started looking in the ploblem. Happily enough it was an easy thing to spot by the steps I used:

1. To check if it was an account-specific error or a general server error I opened other websites that were hosted on the same server. The other sites were working so it was a problem specificly for this hosting account.

2. To check if it was PHP/WP error or Apache error I tried to open a static html file. Opening the file failed so it was some Apache error.. (most probably .htaccess).

Looking through the .htaccess file the end of the file was wrong and had some part of the rewrite rules partially included after the End Of WordPress comment.

... #Normal .htaccess rules
# END WordPress

iteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

# END WordPress

For some reason the last part, starting from ‘iteEngine on’ was added second time. It is highly possible that this was caused by Better WP Security when adding another IP to the blacklisted domains.

If you have any idea what exactly might cause this problem let me know in the comments 😉

P. S.: You can follow me on twitter – @ninarski

WordPress Empty Search Template

WordPress provides you with a nice and easy way to style the search page template using the search.php in your theme. However there is something that is relatively strange related to the searches in WordPress.

If somebody hits the search button without having the search field filled in it gives you the blog archieve template using the index.php. (this is when openinig example.com/?s=)

Solution to this problem is just to tell wordpress that it’s a search (so is_search becomes true) if the s parameter is set but is empty. This should happen on a hook before the posts are requested.

So in your functions.php you can simply put this code

/* Fix for empty search redirect to index.php */
function shtrakeu_fix_empty_search ($query){
  global $wp_query;
  if (isset($_GET['s']) && empty($_GET['s'])){
    $wp_query->is_search=true;
  }
  return $query;
}
add_action('pre_get_posts','shtrakeu_fix_empty_search');

Cheers and follow me on twitter @ninarski

WooCommerce BGN

To ease the stuff even more I’ve created a plugin WooCommerce BGN to simply add Bulgarian lev to the currency list in WooCommerce. This is inspired by my blog post for adding a custom currency to WooCommerce.

You can download WooCommerce BGN here.

Installing:

  1. Download the zip file
  2. Upload it using the Plugins > Add New > Upload
  3. Activate the plugin
  4. Open the WooCommerce options and at the very bottom an option “Bulgarian lev” should have appeared

Happy selling!

Requirements:

WooCommerce

FAQ:

1. I’ve installed the plugin but the prices remain in [any currency here]

Yep. The plugin adds the Bulgarian lev option to WooCommerce. However you have to choose this option from the WooCommerce settings yourself and to position correctly the currency symbol “лв.”

2. The prices are in the wrong format: “лв.12”

You missed to set the position of the currency symbol, didn’t you? Open The WooCommerce Settings and go to the Catalog tab. There is a Pricing Options sections and from the dropdown Currency Position select the desired one. For BGN it is best to be right with space but you can use whatever your heart leads you to.

 

Update to WooCommerce 2.0 and missing information for the product (in the front-end)

WooThemes released the new WooCommerce 2.0 recently and updating seems really charming with the new interface and other listed features.

However sometimes updating won’t be the best thing you can do if your theme is made for 1.6.

In non-technical words – if after the update some of the information is missing or is wrong when seeing it in the front-end this is most related to the theme.

The problem here is that many theme developers (even WooThemes themselves) are using the WC_Product to get the product’s data. The problem is that you can no longer create an instance of WC_Produc by post ID. This is probably related to the new function get_product() which I think is really poorly documented in their official docs for some thing to put in the new release information.

In my case for a theme made from WooThemes themselves I had:

//inside the loop
$_product = &new WC_Product( $loop->post->ID );

This would no longer work so I changed it to

$_product = get_product(  $loop->post->ID );

Which fixed it right away.

In my opinion it would be good if they could update their own existing themes to work with the new version insead of just not caring what their end-users would end up with.