database interactions using OOP

This is potentially a fairly involved question, so I may not be totally rigorous in my answer, but this should give you a good start when creating and deleting tables.. Include the following lines in your plugin _construct() function of class_my_plugin.php: if (is_admin()){ //manage the the database tables when registering or deactivating register_activation_hook($loc, array($this, ‘your_plugin_installation’)); … Read more

Use object in template part

As of WordPress 5.5 you can pass variables to template parts by passing them in an array to the third argument of get_template_part(): foreach ($categories as $category) { get_template_part( ‘temp-parts/loop/blcnr_loop’, null, [ ‘category’ => $category ] ); } These variables will populate an $args variable accessible from the the template: echo $args[‘category’]->name;

Advice on plugin structure

You should limit your helper functions. In a rather big plugin I am writing currently (~50 classes) I have just three helper functions, placed in the plugin main file: /** * Delete plugin option on deactivation. * * @return boolean */ function t5_delete_option() { return delete_site_option( ‘plugin_t5’ ); } /** * Load a class from … Read more

Registering and using actions which return results in a Plugin class

Some actions hooks need to be fired on specific events. Try this code: class SpektrixPlugin { public function __construct(){ add_action(‘init’, array(&$this, ‘init’)); } public function spektrix_list_events($getPrices = FALSE) { $api = new SpektrixApiClient(); return $api->getAllEvents($getPrices); } public function init(){ add_action( ‘spektrix_list_events’, array ( $this, ‘spektrix_list_events’ )); } } $SpektrixEvents = new SpektrixPlugin(); I’ve tested this … Read more

Plugin activation hook in an abstract class

The best way to get around this problem is not creating that abstract class. Never combine object creation with business logic in the same class. This is called a statically linked dependency and usually a flawed design, because you cannot change it on runtime (for tests or per hooks, for example). Avoid inheritance. Separation of … Read more

How to get media objects

Have you tried to use get_children() function? Edit: All media attached to any post is treated as children of it. The function wp_get_attachment_image() will only return images. With the function get_children() you’ll be able to get all the media, images or anything else. In the above link, you can find some examples.

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