Override core woocommerce class

1) There is a hook for the first one: You can use the apply_filters Their code: apply_filters( ‘woocommerce_get_discounted_price’, $price, $values, $this ); Your code: add_filter( ‘woocommerce_get_discounted_price’, function($price, $values, $this) { //return whatever you want },10,3 ); //*PHP 5.3 and up you can pass whatever function you want into the second parameter of the add_filter function. … Read more

Using spl_autoloading within WordPress plugin

I think the problem here is that you’re using a relative path in autoload.php inside the src/ subfolder, Try for example to modify your src/autoload.php to use absolute paths instead: \spl_autoload_register( function( $class ) { $dir = plugin_dir_path( __FILE__ ); $file = str_replace( ‘\\’, “https://wordpress.stackexchange.com/”, $class ) . ‘.php’; $path = $dir . $file; if( … Read more

Upload files programmatically to users

it was just missing $upload_dir[‘path’] . “https://wordpress.stackexchange.com/” . $upload_file_name in $uploaded_file[‘attach_id’] = wp_insert_attachment($attachment, $upload_dir[‘path’] . “https://wordpress.stackexchange.com/” . $upload_file_name); require_once(ABSPATH . ‘wp-admin/includes/image.php’); //Generate the metadata for the attachment, and update the database record. $attach_data = wp_generate_attachment_metadata($uploaded_file[‘attach_id’], $upload_dir[‘path’] . “https://wordpress.stackexchange.com/” . $upload_file_name);

wp_enqueue_scripts not working inside shortcode

$shortcode = new Shortcode_Class(); class Shortcode_Class { /** * If you should add the script or not * * @var bool */ private $addScript = false; public function __construct() { add_shortcode(‘test_shortcode’, array($this, ‘shortcode_content’)); // wp_enqueue_scripts // If you use the below the CSS and JS file will be added on everypage // add_action( ‘wp_enqueue_scripts’, array($this, … Read more

Creating search filter through plugin

You can add more than one filter call, so each of your custom meta_key‘s can be handled in separate code and when executed will cumulatively adjust the query. Perhaps look at the pre_get_posts filter rather than parse_query (although the solution I’m suggesting could possibly work in either case). pre_get_posts gives you access to the current … Read more

Add a Custom Permalink option in the Permalink Admin Screen?

To hook your custom plugin settings into the Permalink settings, use the following: add_action(‘admin_init’, ‘wpse_237141_custom_permalink_section’); function wpse_237141_custom_permalink_section() { add_settings_section( ‘your_custom_id’, // ID ‘Your Plugin Settings’, // Section title ‘your_callback’, // Callback for your function ‘permalink’ // Location (Settings > Permalinks) ); } You’ll need to fill in the blanks to display your own custom functions, … Read more

OOP: Display warning and deactivate the plugin if PHP version is less than 5.4

Here’s my bare-bones template that I use when creating a plugin with OOP. Feel free to modify to your liking. Template if ( !defined( ‘ABSPATH’ ) ) exit; // Exit if accessed directly if ( !class_exists( ‘MyPluginName’ ) ) { class MyPluginName { public function __construct() { // Call your actions/filters here add_action( ‘tag’, array( … Read more