Settings API with arrays example

Short answer: your name attribute values must use the schema option_name[array_key]. So, when you use … <input name=”option_name[key1]”> <input name=”option_name[key2]”> … you get an array as option value in your validation function: array ( ‘key1’ => ‘some value’, ‘key2’ => ‘some other value’ ) PHP does that for you, this is not a WordPress feature. … Read more

What are the differences between WPINC and ABSPATH?

They are defined as follows: define( ‘ABSPATH’, dirname(dirname(__FILE__)) . “https://wordpress.stackexchange.com/” ); define( ‘WPINC’, ‘wp-includes’ ); dirname is a PHP function that returns the path of the parent directory, and wp-includes is pretty self explanatory. I would say ABSPATH is better because it’s one of the first things WP loads and it also looks better:) But … Read more

Should I use custom post types or a custom database tables for plugin development?

You should be skeptical of anyone who says that there is a single “right” way. The right way depends on the situation. Using the CPT infrastructure has a number of notable benefits: You get the Dashboard UI for free You automatically take advantage of WP’s caching, including any persistent cache plugins that the installation may … Read more

How can you check if you are in a particular page in the WP Admin section? For example how can I check if I am in the Users > Your Profile page?

The way to do this is to use the ‘admin_enqueue_scripts’ hook to en-queue the files you need. This hook will get passed a $hook_suffix that relates to the current page that is loaded: function my_admin_enqueue($hook_suffix) { if($hook_suffix == ‘appearance_page_theme-options’) { wp_enqueue_script(‘my-theme-settings’, get_template_directory_uri() . ‘/js/theme-settings.js’, array(‘jquery’)); wp_enqueue_style(‘my-theme-settings’, get_template_directory_uri() . ‘/styles/theme-settings.css’); ?> <script type=”text/javascript”> //<![CDATA[ var template_directory … Read more

What’s the preferred method of writing AJAX-enabled plugins?

the “safer and cleaner” way would be to use admin-ajax.php that comes with wordpress and wp_ajax hook to call your processing function from your plugin file and use wp-nonce to check the integrity of the call. for example: your ajax JQuery call would be <script type=”text/javascript” > jQuery(document).ready(function($) { var data = { action: ‘ACTION_NAME’, … Read more

Creating a table in the admin-style?

This is what I generally use: <table class=”widefat fixed” cellspacing=”0″> <thead> <tr> <th id=”cb” class=”manage-column column-cb check-column” scope=”col”></th> // this column contains checkboxes <th id=”columnname” class=”manage-column column-columnname” scope=”col”></th> <th id=”columnname” class=”manage-column column-columnname num” scope=”col”></th> // “num” added because the column contains numbers </tr> </thead> <tfoot> <tr> <th class=”manage-column column-cb check-column” scope=”col”></th> <th class=”manage-column column-columnname” scope=”col”></th> … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)