Blog

  • Hide WordPress 7.0 AI Options

    This first snippet goes in your wp-config file and disables the connection options listed under the sub-menu.

    // Hide AI options.
    define( 'WP_AI_SUPPORT', false );

    This next snippet goes in your functions.php file and hides the “Connectors” submenu page from the dashboard entirely.

    function prefix_remove_AI_menu(){
         remove_submenu_page( 'options-general.php', 'options-connectors.php' );
    }
    add_action( 'admin_menu', 'prefix_remove_AI_menu' );
  • Modify post content to insert text after h2

    This code snippet filters the_content(), uses regex to find the first h2 and insert content from an ACF custom field named affiliate_text from the options page.

    /**
     * Custom function to modify the post content by inserting text after the first <h2> tag.
     *
     * @param string $content The original post content.
     * @return string Modified post content.
     */
    function affiliate_h2($content) {
        global $post;
    
        // Check if we are on a single post/page and if the post content is not empty
        if (is_single() && !empty($content)) {
            // Retrieve the custom field value
            $custom_field_value = get_field('affiliate_text', 'option');
            
            // Use regex to find the first <h2> tag
            $pattern = '/<h2[^>]*>.*?<\/h2>/i';
            preg_match($pattern, $content, $matches);
    
            // Check if an <h2> tag was found
            if (!empty($matches)) {
                // Create a paragraph tag with the custom field value
                $custom_paragraph = '<p>' . $custom_field_value . '</p>';
    
                // Insert the custom paragraph before the first <h2> tag
                $content = preg_replace($pattern, $custom_paragraph . '$0', $content, 1);
            }
        }
    
        return $content;
    }
    // Hook the custom function to the_content filter
    add_filter('the_content', 'affiliate_h2');
  • Uniformly replace dashes in PHP in order to explode

    This came about because I was trying to split a title string at the dash within WordPress. Not-so-quickly realizing that I had to account for all the different types of programmatic dashes out there that all look the same visibly. May this code snippet get you across the finish line faster to replace dashes easier.

    // Make sure everything is using the same character set.
    $title1 = iconv(mb_detect_encoding( get_the_title( $post_ID ), mb_detect_order(), true ), "UTF-8", get_the_title( $post_ID ) );
    
    // Convert the raw string and not HTML encoded output.
    $title2 = html_entity_decode( $title1, ENT_NOQUOTES | ENT_HTML5, "UTF-8" );
    
    // Uniformly replace dashes.
    $dashes = ['–','–','—','-'];
    $title3 = str_replace( $dashes, '-' , $title2 );
    
    $subtitle = explode( '-', $title3 );
  • Add Image within Nav Item Link

    The outcome of this snippet will be an image with the size “square” (assuming you have this in your functions.php) followed by the nav item title. This is also assuming you have attached a custom image field using ACF with the name of associated_item_image.

    // Add image within menu item link.
    add_filter('wp_nav_menu_objects', 'prefix_wp_nav_menu_objects', 10, 2);
    function prefix_wp_nav_menu_objects( $items, $args ) {
    	foreach( $items as $item ) {
    		$associated_img = get_field( 'associated_item_image', $item );
    
    		if( $associated_img ) {
    			$item->title = $item->title . '<img class="associated-img" src="' . $associated_img['sizes']['square'] . '" alt="' . $associated_img['alt'] . '" /> ';
    		}
    	}
    	return $items;
    }