Category: WordPress

  • Change WooCommerce order emails subject

    /*
     * goes in theme functions.php or a custom plugin
     *
     * Subject filters: 
     *   woocommerce_email_subject_new_order
     *   woocommerce_email_subject_customer_processing_order
     *   woocommerce_email_subject_customer_completed_order
     *   woocommerce_email_subject_customer_invoice
     *   woocommerce_email_subject_customer_note
     *   woocommerce_email_subject_low_stock
     *   woocommerce_email_subject_no_stock
     *   woocommerce_email_subject_backorder
     *   woocommerce_email_subject_customer_new_account
     *   woocommerce_email_subject_customer_invoice_paid
     **/
    add_filter('woocommerce_email_subject_new_order', 'change_admin_email_subject', 1, 2);
    
    function change_admin_email_subject( $subject, $order ) {
    	global $woocommerce;
    
    	$blogname = wp_specialchars_decode(get_option('blogname'), ENT_QUOTES);
    
    	$subject = sprintf( '[%s] New Customer Order (# %s) from Name %s %s', $blogname, $order->id, $order->billing_first_name, $order->billing_last_name );
    
    	return $subject;
    }

    More here

  • Hide WooCommerce payment or shipping methods conditionally and programatically

    Payment gateways: woocommerce_available_payment_gateways filter hook

    Shipping gateways: woocommerce_package_rates filter hook

    Read more here

  • Install PHPUnit with WP

    Local development

    Guide here

    PHPUnit versions vs. WP versions

    You may need additional libraries and help. Use Google for that.

  • Adding an admin submenu page to a custom post type CPT

    add_submenu_page(
        'edit.php?post_type=wnm_funds',
        'Fund Settings', /*page title*/
        'Settings', /*menu title*/
        'manage_options', /*roles and capabiliyt needed*/
        'wnm_fund_set',
        'CALLBACK_FUNCTION_NAME' /*replace with your own function*/
    );

    More here

  • WooCommerce admin settings options

    Extending the WooCommerce Documentation page: https://woocommerce.com/document/settings-api/

    'option_name' => array(
    	'type' => 'title|sectionend|text|password|datetime|datetime-local|date|month|time|week|number|emailk|url|tel|color|textarea|radio|checkbox|select|multiselect|image_width|single_select_page|single_select_page_with_search|single_select_country|multi_select_countries|relative_date_selector', //Required
    	'id'	 => 'Unique field ID',
    	'title' => 'Title for your option shown on the settings page',
    	'class' => 'Space separated classes for the input',
    	'css' => 'Space and ; separated CSS rules added line to the input',
    	'default' => 'Default value for the option',
    	'desc' => 'Description for your option shown on the settings page',1
    	'desc_tip' => 'Description for your option shown on the settings page',
    	'placeholder' => 'Placeholder for the text field types',
    	'suffix' => 'Content to place after text field field types',
    	'value' => 'Do not set it as Woo will set it for you',
    	'custom_attributes' => 'Array of type key => value that will be appended in the field construction sequence', //Eg. array( 'max' => '100', 'maxlength' => 3 )
    	'label' => 'Label', // checkbox only
         	'options' => array(
              'key' => 'value'
         	) // array of options for select/multiselects only
    );

    to create your own type of field, there is an available action that takes in 1 parameter – $value

    woocommerce_admin_field_{$type}

    eg. usage:

    add_action( 'woocommerce_admin_field_my_custom_type', 'my_custom_field_type', 10, 1 );
    function my_custom_field_type( $value ) {
    	echo '<field value="' . $value . '">Label</field>';
    }
  • Remove WordPress users via WP CLI

    Remove users with customer role:

    # Delete user 123 and reassign posts to user 567
    $ wp user delete 123 --reassign=567 --yes
    Success: Removed user 123 from http://example.com
    
    # Delete all contributors and reassign their posts to user 2
    $ wp user delete $(wp user list --role=customer --field=ID) --reassign=2 --yes
    Success: Removed user 813 from http://example.com
    Success: Removed user 578 from http://example.com
    
    # Delete all contributors in batches of 100 (avoid error: argument list too long: wp)
    $ wp user delete $(wp user list --role=customer --field=ID | head -n 100) --reassign=567 --yes

    Source: https://developer.wordpress.org/cli/commands/user/delete/

  • Gutenberg attributes to console

    Log attributes to console using the following:

    wp.data.select( 'core/block-editor' ).getSelectedBlock().attributes;

    Source: https://notlaura.com/log-gutenberg-blocks-attributes-to-console/