How to add custom fields to menu items

Have you ever felt the need to add custom fields to your menu items? It seems there isn’t a straight forward way to do this but there is certainly a way.

I wrote a script that will help you accomplish this, you can find it on github.

You only need to include both of the files found in the repository into your theme or plugin and require one of them, Menu.php.
As in the example:

require_once dirname( __FILE__ ) . '/Menu.php';

Then you can just build an array of the custom fields you need and pass it in as the argument of the Menu object.

$fields = array(
        '_mycustom_field_1' => array(
            'label' => __( 'Custom field 1', 'domain' ),
            'element' => 'input',
            'sanitize_callback' => 'sanitize_text_field',
            'attrs' => array(
                'type' => 'text',
                ),
            ),
        '_mycustom_field_2' => array(
            'label' => __( 'Custom field 2', 'domain' ),
            'element' => 'select',
            'sanitize_callback' => 'sanitize_text_field',
            'options' => array(
                'option-1' => __( 'Option 1', 'domain' ),
                'option-2' => __( 'Option 2', 'domain' ),
                ),
            ),
        );
 
        // Menu Management custom fields.
        new \Lucymtc\Theme\Menu( $fields );

You will probably need to add some styling. Bellow you can find the visual result.
Hope you find this script useful!

Add custom functionality to Contact Form 7

You can add custom functionality on Contact Form 7 plugin after different actions have taken place, for example when an email has been sent. Contact Form 7 triggers some JavaScript events that you can easily access, here you will find an example using JQuery.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
if( $('.wpcf7').length > 0 ) {
 
  $(document).on('spam.wpcf7', function () {
    console.log('submit.wpcf7 triggered!');
  });
 
  $(document).on('invalid.wpcf7', function () {
    console.log('invalid.wpcf7 triggered!');
  });
 
  $(document).on('mailsent.wpcf7', function () {
    console.log('mailsent.wpcf7 triggered!');
  });
 
  $(document).on('mailfailed.wpcf7', function () { 
    console.log('mailfailed.wpcf7 triggered!');
  });
}

WordPress Development Tools and Resources

This list may grow or change with time.

GitHub updater
Plugin to enable automatic updates to your GitHub or Bitbucket hosted WordPress plugins and themes.
https://github.com/afragen/github-updater

Developer
Plugin that will help you optimize your development environment by making sure that you have all the essential tools and plugins installed.
https://wordpress.org/plugins/developer/

Theme Check
Test your theme for all the latest WordPress standards and practices
https://wordpress.org/plugins/theme-check/

Log Deprecated Notices
This plugin logs the usage of deprecated files, functions, and function arguments. Identifies where the deprecated functionality is being used and it will offer an alternative if it’s available. It also logs incorrect function usage.
https://wordpress.org/plugins/log-deprecated-notices/

Monster Widget
Provides a quick and easy method of adding all core widgets to a sidebar for testing purposes
https://wordpress.org/plugins/monster-widget/

Debug Bar
Plugin that adds a debug menu to the admin bar that shows helpful debugging information.
https://wordpress.org/plugins/debug-bar/

List Code Validation Tools
http://codex.wordpress.org/Validating_a_Website#HTML_-_Validation

Theme Framework: Hybrid Core
This is not a parent Theme. It’s a framework to build your own themes.
https://github.com/justintadlock/hybrid-core

WP Test
Import dummy content to test. The content has many different styles, lists, floating images… so you can see how you all the elements are displaying in your theme.
http://wptest.io/

How to add dynamic values into Contact Form 7 inputs

Contact Form 7 is a great plugin to add forms to your site, but these forms contain static inputs and sometimes we require to add inputs with dynamic values like a post ID or PHP variables like GET and POST.

This is very easy to do with the plugin Contact Form 7 Dynamic Text Extension.

This plugin adds two additional tag types to the Contact Form 7 plugin, these two types are Dynamic Text field and Dynamic Hidden field, the dynamic value for a scpecif input is generated throught a shortcode.

The plugin has it’s own shortcodes integrated, but you can create your own.

Let me put a very simple example:

Lets say you want to add a hidden input in your form which value should be the current post ID.
If you create your own shortcode it would look like this:

function cf7_add_post_id(){
 
    global $post;
    return $post->ID;
}
 
add_shortcode('CF7_ADD_POST_ID', 'cf7_add_post_id');

And the dynamic field in your form setup would look like this:

[dynamictext my-filed-name "CF7_ADD_POST_ID"]

The other way would be to use the plugin’s shortcodes.
In this case we want to have the current post ID so you would only have to add your dynamic field in the form setup like this:

[dynamictext my-filed-name "CF7_get_post_var key='ID'"]

The plugin has shortcodes for GET, POST, blog info, post info, current URL, custom fields, current user info, referrer URL.
You have all the details in the plugin’s page on wordpress.org

WP Import All, import any kind of data to your WordPress database

wp-all-importWp All Import is a great plugin to import any kind of data into your WordPress site.

You can import XML, CSV/Excel, JSON, SQL and in any kind of structure for posts, pages or custom post types.
With a simple and very easy to use drag and drop system to indicate what information is for what data base field.

It will import your images and also can autodetect the custom fields for the postmeta table.
They have an advanced version that supports Advanced Custom Fields and WooCommerce products.

The free version is very limited, it won’t work for the images or custom fields, wich I think you probably need in almost 100% of the cases. So if you are not a developer this plugin is worth the price.

I will tell you an example of when this plugin can be usefull. Imagine you run (or you have a client that runs) a portal of classified ads, a property portal or something similar, and you constantly have to import files with data from other external websites where each one sends you the data in a different structure and format. This plugin is great for that as you can also save the settings for each import as a template, so you only need to set up the import once and then load the template each time you need to import new data sent by the same source.

Check out the video on their wordpress.org page

Note: I used it in a site that manages classified ads, the user is able to create an ad, after that he receives a notification by email confirming the ad has been created. When I did the import of 1000 ads asigned to an author, the server tried to send 1000 email notifications. So be sure to turn off email notifications on this case scenario.

How to load template files in a plugin

Template files are usually used in themes and child themes, but you can also load template files from a plugin.

It’s not to difficult to create your own template loader, but you can also find the Gamajo_Template_Loader on GitHub by Gary Jones.

To use this class:

1. Create a file in your plugin and copy the Gamajo_Template_Loader class into it.
2. Create a new class that extends the Gamajo_Template_Loader. Find an example here
3. Override the class properties for your plugin, these are:
$filter_prefix, $theme_template_directory, $plugin_directory.

You can also find a full plugin example by Pippin Williamson from pippinsplugins.com using the Gamajo_Template_Loader

Rename table prefix on existing WordPress site

Follow these few steps to rename the table prefix of your existing WordPress site.

Edit wp-config.php and change

$table_prefix = 'wp_';

to any other prefix, try to make it abstract and hard to be guessed by anyone, for example:

$table_prefix = 'v5r33w67ee_';

Rename the tables in your database, these are the SQL queries to rename the core tables:

RENAME table `wp_users` TO `v5r33w67ee_users`;
RENAME table `wp_usermeta` TO `v5r33w67ee_usermeta`;
RENAME table `wp_comments` TO `v5r33w67ee_comments`;
RENAME table `wp_commentmeta` TO `v5r33w67ee_commentmeta`;
RENAME table `wp_links` TO `v5r33w67ee_links`;
RENAME table `wp_options` TO `v5r33w67ee_options`;
RENAME table `wp_posts` TO `v5r33w67ee_posts`;
RENAME table `wp_postmeta` TO `v5r33w67ee_postmeta`;
RENAME table `wp_terms` TO `v5r33w67ee_terms`;
RENAME table `wp_term_taxonomy` TO `v5r33w67ee_term_taxonomy`;
RENAME table `wp_term_relationships` TO `v5r33w67ee_term_relationships`;

Add the queries to rename any other existing table that have been created from other plugins or themes.
You can also rename the tables in phpMyAdmin, by clicking on the operations tab

Update the references to the old table prefix in the usermeta and options table running these SQL queries.

UPDATE `v5r33w67ee_usermeta` SET `meta_key` = REPLACE( `meta_key`, 'wp_', 'v5r33w67ee_' );
UPDATE `v5r33w67ee_options` SET `option_name` = 'v5r33w67ee_user_roles' WHERE `option_name` = 'wp_user_roles';