Create table in database when activating plugin

I think you’re missing a comma after product_categoy_url varchar(500) NOT NULL then if still not working, be aware that register_activation_hook( __FILE__, ‘gg_create_table’); will work only if placed in the main plugin file, not in an included file. To execute the activation function from a different file you’ve to define in the main file: define(‘MY_PLUGIN_PATH’,__FILE__); than … Read more

Can’t get JS code to work with shortcode

You need to return the Shortcode generated string, not echo, like this: function gg_product_front_end() { wp_register_script( ‘gg_loadProducts_frontEnd’, plugins_url( ‘js/front_end.js’, __FILE__ ), array( ‘jquery’ )); wp_enqueue_script( ‘gg_loadProducts_frontEnd’ ); return ‘<p id=”test”>Test!</p>’; } Also, you need to call the JavaScript function, like this: function gg_loadProducts_frontEnd() { console.log( ‘Test!’ ); } gg_loadProducts_frontEnd(); Otherwise Test! will not be logged … Read more

Unable to load stylesheets and scripts to plugin settings page

The way to go was to use wp_enqueue_style() and wp_enqueue_script functions when admin_enqueue_scripts hooks. To register styles and scripts with wp_register_style and wp_register_script respectively is optional, but has some good advantages as nicely explained here. Here’s the working script: <?php /* Plugin Name: Awesome Slide Show */ // admin_init // ************************************************************************************************ function wp_ss_plugin_admin_init_cb() { // … Read more

When is is_admin() available?

When is is_admin() available? Pretty much everywhere. The only example I can think of where it wouldn’t be available, is super early in files such as wp-config.php, and some drop-ins. But by the time an mu-plugin, a theme, or a plugin is loaded it’s present. Now, it would work to wrap the inner workings of … Read more

Custom preview_post_link not working for draft post

I encountered the same issue and after some research, I came across the following solution. For context, there is a bug on gutenberg and there is no platform specific way to change the preview link yet. Add the following code to your functions.php: // Workaround script until there’s an official solution for https://github.com/WordPress/gutenberg/issues/13998 function fix_preview_link_on_draft() … Read more

How to add notification in the sidebar of the administration panel when a Custom post type is created? [duplicate]

It is shockingly simple: you have to dyamically set the admin page title, adding a specific markup: <span class=”awaiting-mod count-3″> <span class=”pending-count”>3</span> </span> So for example if you want to show the number of custom posts you have to do something like this when declaring the options page: $notif_count = wp_count_posts(‘my_post_type’); //insert your post type … Read more

Use php libarary in wordpress plugins

You can always use the Composer when making a plugin and require all its dependencies. But as MikenGarrett said, finally you have to bundle all the required vendors into the plugin as a whole package. There are WordPress repository, containing /vendor/ directory1 and their development environment containing composer.json file declaring production-dependencies (‘require’)2. But using Composer … Read more

Can I differentiate between “Delete Post Permanently” and “Empty Trash” and do something for each accordingly?

So as leymannx pointed out in his comment, the “delete_post” hook actually handles both scenarios I was looking for, as it seems to know to run a “for each post deleted, do x” when “Empty Trash” is clicked and will also “do x” when “Delete Post Permanently” is clicked! My final code ended up looking … Read more

What is the use of get_option method

Well, as you can see in the contributed example to the WordPress Reference, it is not that add_settings_field magically knows about a setting being true or false, and it is not limited to checkboxes (boolean flags): Instead, the function signature states clearly that the $callback parameter it is required. And the introduction says: The $callback … Read more