Magento Engineer
social
Journal

Programming Blogs to Improve Your Coding Skills!

The industry's top wizards and other experts share their advice and research findings.

Create wprdpress posts by csv with featured image by code.

<?php
// Include WordPress bootstrap file
require_once 'wp-load.php';

// Path to your CSV file
$csv_file = '1.csv';

// Function to download and attach the image to the post
function attach_featured_image($post_id, $image_url) {
    // Download the image from the URL
    $image_contents = file_get_contents($image_url);
    
    if ($image_contents !== false) {
        // Get the filename from the URL
        $filename = basename($image_url);
        
        // Upload the image to the WordPress media library
        $upload = wp_upload_bits($filename, null, $image_contents);
        
        if (!$upload['error']) {
            // Prepare the attachment data
            $attachment_data = array(
                'post_mime_type' => $upload['type'],
                'post_parent' => $post_id,
                'post_title' => sanitize_file_name(pathinfo($filename, PATHINFO_FILENAME)),
                'post_content' => '',
                'post_status' => 'inherit'
            );
            // Insert the attachment into the database
            $attach_id = wp_insert_attachment($attachment_data, $upload['file']);
            // Set the featured image
            if (!is_wp_error($attach_id)) {
                set_post_thumbnail($post_id, $attach_id);
                return true;
            } else {
                return false;
            }
        }
    }
    return false;
}




// Open the CSV file for reading
if (($handle = fopen($csv_file, 'r')) !== false) {
    // Loop through each row in the CSV file
    while (($data = fgetcsv($handle, 1000, ',')) !== false) {
        // Extract data from CSV row
        $post_title    = $data[0]; // Assuming first column is post title        
        $custom_field1 = $data[1]; // Assuming fourth column is custom field 1 value
        $custom_field2 = $data[2]; // Assuming fifth column is custom field 2 value
        $custom_field3 = $data[3]; // Assuming fifth column is custom field 2 value
        $custom_field4 = $data[4]; // Assuming fifth column is custom field 2 value
        $image_url     = $data[5]; // Assuming third column is image URL
        $post_content  = $data[6]; // Assuming second column is post content        
        
        // Add more custom fields as needed
        //title   seo_title   seo_description keyword published_at    featured_image  content

        // Create post array with custom fields

        $existing_post = get_page_by_title($post_title, OBJECT, 'post');

        if ($existing_post) {
                    // Post already exists, update the post content and featured image
                    $post_id = $existing_post->ID;
                    $post_data = array(
                        'ID'           => $post_id,
                        'post_content' => $post_content,
                    );
                    wp_update_post($post_data);

                    // Attach featured image if URL is provided
                    if (!empty($image_url) && !is_wp_error($post_id)) {
                        attach_featured_image($post_id, $image_url);
                    }
                    
                    echo "Post updated: $post_title<br>";
                } else {

                    $new_post = array(
                        'post_title'   => $post_title,
                        'post_content' => $post_content,
                        // Add more fields as needed
                        'post_status'  => 'publish', // You can change post status if needed
                        'post_author'  => 1, // Change the author ID if needed
                        'post_type'    => 'post', // Change the post type if needed
                        'meta_input'   => array(
                            'seo_title'       => $custom_field1,
                            'seo_description' => $custom_field2,
                            'keyword'         => $custom_field3,
                            'published_at'    => $custom_field4,
                            'featured_img'    => $image_url,
                            // Add more custom fields as needed
                        ),
                    );
                }

        // Insert the post into the database
        $post_id = wp_insert_post($new_post);

        // Attach featured image if URL is provided
        if (!empty($image_url) && !is_wp_error($post_id)) {
            attach_featured_image($post_id, $image_url);
        }
        // Handle errors if any
        if (is_wp_error($post_id)) {
            $errors = $post_id->get_error_messages();
            foreach ($errors as $error) {
                echo $error . '<br>';
            }
        }
    }
    // Close the CSV file
    echo "CSV uploaded successfully!";
    fclose($handle);
} else {
    echo 'Error opening CSV file';
}
?>

Ad comes here

How to Move Your Blog/Website from WordPress.com to WordPress.org?

Many beginners often start with WordPress.com, but they soon realize its limitations and want to switch to the self-hosted WordPress.org platform.

In this step by step guide, we’ll show you how to properly move your blog from WordPress.com to WordPress.org.

Step1 - Login in the wordpress.com admin.
Step2 - Check "All-in-One WP Migration" from left sidebar and if it is not availble in your website then ping to  WordPress.com support team to add this.

Step3 - Open All-in-One WP Migration and click on export.

Step4 - Here you will see multiple export options, now choose FILE.
Step5 - Download will be started.

Step6 - The exported file will be in "wpress" format.
Step7 - Now run below command to extract and you will get Database and all wordpress files including plugin,theme,uploads.
Command: npx wpress-extract migration.wpress

Step8- Change wp-config.php and URL's from database and upload on new server.

That's it.
:) Enjoy

Ad comes here
Ad comes here

How do I add a external CDN CSS OR jQuery in WordPress theme header?

Rather then loading the stylesheet in your header.php file, you should load it in using wp_enqueue_style. In order to load your main stylesheet, you can enqueue it in functions.php

function bootstarp_to_head() {
    echo "<link href='https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css' rel='stylesheet' type='text/css'>";
}
add_action( 'wp_head', 'bootstarp_to_head' );

This will includes in theme header.

Ad comes here

How do I add a custom stylesheet in WordPress?

Rather then loading the stylesheet in your header.php file statically, you should load it in using wp_enqueue_style. In order to load your main stylesheet, you can enqueue it in functions.php

function theme_styles()  
{
    wp_register_style( 'responsive', get_template_directory_uri() . '/assets/css/responsive.css' );
    wp_enqueue_style('responsive');
    
}
add_action('wp_enqueue_scripts', 'theme_styles');

Ad comes here
Error's and solution's
Update Cookies Preferences