When would I use either function for plugins?

Return values of the three mentioned functions 1. plugin_dir_path( __FILE__ ) returns the servers filesystem directory path pointing to the current file, i.e. something along the lines of /home/www/your_site/wp-content/plugins/your-plugin/includes/ This can be used for loading PHP files. 2. plugins_url() returns the web address of the current WordPress installation’s plugin folder, i.e. something along the lines … Read more

How to use PanelColorSettings in custom Gutenberg block?

First you need to import the component – const { PanelColorSettings, } = wp.editor; then inside the InspectorControls you call the component <PanelColorSettings title={ __( ‘Color Settings’ ) } colorSettings={ [ { value: color, onChange: ( colorValue ) => setAttributes( { color: colorValue } ), label: __( ‘Background Color’ ), }, { value: textColor, onChange: … Read more

How do I programatically insert a new menu item?

Before being printed, all the menu items get run through a filter. You can target the wp_nav_menu_items filter to tack things on to the menu: // Filter wp_nav_menu() to add additional links and other output function new_nav_menu_items($items) { $homelink = ‘<li class=”home”><a href=”‘ . home_url( “https://wordpress.stackexchange.com/” ) . ‘”>’ . __(‘Home’) . ‘</a></li>’; // add … Read more

Export data as CSV in back end with proper HTTP headers

Do not point the URL to admin.php, use admin-post.php instead: ‘<a href=”‘ . admin_url( ‘admin-post.php?action=print.csv’ ) . ‘”>’ In your plugin register a callback for that action: add_action( ‘admin_post_print.csv’, ‘print_csv’ ); function print_csv() { if ( ! current_user_can( ‘manage_options’ ) ) return; header(‘Content-Type: application/csv’); header(‘Content-Disposition: attachment; filename=example.csv’); header(‘Pragma: no-cache’); // output the CSV data } … Read more