Formatting of curly brackets array from WP database to get more readable output

This is a serialized value, so you should run it through maybe_unserialize() or just unserialize() before you edit it. When you work in plugins or themes, always use the API: update_option() and get_option() for example. These functions will un/serialize the values for you, so you don’t have to worry about the database. See also: Settings … Read more

Get file headers in custom file

Use get_file_data( $file, $headers ): $file_data = get_file_data( __FILE__, array ( ‘Plugin Name’ ) ); echo “the name is ” . $file_data[0]; Make sure the first parameter points to an existing file. It will find all lines that are formatted like regular plugins headers or the headers of a style.css. In my plugin T5 Opera … Read more

When is it appropriate to put functions on page template vs. functions.php?

If it is a custom theme for your site then there would no great speed or security improvements by moving functions. On the other hand if you want to distribute the theme then I recommend having only the minimum php in the page templates files and having the rest of the functions organized in different … Read more

How to ensure “the_content” filter runs only for the main displayed content?

Try adding a condition to your filtering function that checks your post type against get_post_type. if ( ‘book’ == get_post_type() ) If you wish to apply this filter to pages as well, try is_singular() and include your custom post type(s) as an argument. is_singular(‘book’); This will return true if any of the following conditions are … Read more

How to add a link to WordPress Plugin install Listing

This is the filter: “plugin_install_action_links”. You can find this in /wp-admin/includes/class-wp-plugin-install-list-table.php (currently) at line 434: $action_links = apply_filters( ‘plugin_install_action_links’, $action_links, $plugin ); Use it like: add_filter( ‘plugin_install_action_links’, ‘my_custom_links’, 10, 2 ); function my_custom_links( $action_links, $plugin ){ $action_links[] = ‘<a href=””>……</a>’; return $action_links; }

How to Use WordPress Color Picker API in Custom Post Type Metabox

I’ve created a metabox once with color picker. You need to include color picker style and script in your admin_enqueue_scripts hook, and then initialize it with $(‘.color-picker’).wpColorPicker(); From my gist: <?php add_action( ‘admin_enqueue_scripts’, ‘mytheme_backend_scripts’); if ( ! function_exists( ‘mytheme_backend_scripts’ ) ){ function mytheme_backend_scripts($hook) { wp_enqueue_media(); wp_enqueue_style( ‘wp-color-picker’); wp_enqueue_script( ‘wp-color-picker’); } } add_action( ‘add_meta_boxes’, ‘mytheme_add_meta_box’ ); … Read more

Custom Rewriting to Plugin with Parameters

This approach should work for you: <?php /** * Plugin Name: Rewrite Shortcode **/ add_shortcode( ‘myplugin’, ‘mp_shortcode’ ); function mp_shortcode(){ return ‘<p>Filter: ‘ . get_query_var( ‘filter’ ) . ‘</p>’; } add_action( ‘init’, ‘mp_rewrite’ ); function mp_rewrite(){ $post_id = 2; add_rewrite_rule( ‘myplugin/([^/]+)/?$’, ‘index.php?p=’ . $post_id . ‘&filter=$matches[1]’, ‘top’ ); add_rewrite_tag( ‘%filter%’, ‘([^/]+)’ ); } ?> I … Read more

Is it save to require plugin.php early to be able to use get_plugin_data() earlier?

At first glance it works and I cannot see any implications but maybe you are aware of some? You say that you are not getting errors, which I would have expected but it looks as though WordPress uses require_once() so you are probably safe: 39 /** WordPress Plugin Administration API */ 40 require_once(ABSPATH . ‘wp-admin/includes/plugin.php’); … Read more

Escaping built-in WP function return strings

Escaping is used to produce valid HTML or other formats, and it depends on context. Escaping a url in something like <a href=”https://wordpress.stackexchange.com/questions/215822/<?php echo $url?>”…. is needed in order to replace any “&” characters with & (although browsers will most likely fix it for you if you don’t do it). Escaping a url in an … Read more