Git Product home page Git Product logo

set-post-thumbs's Introduction

Set Post Thumbs

A simple WP-CLI command to query posts for their content and set the first found image as the post thumbnail.

Installation

Preferred

There are two preferred ways to install this package:

First, using WP-CLI's package installer command. Simply run wp package install jmichaelward/set-post-thumbs in the command line, then you're good to go.

Second, you can similarly install it using Composer. In the terminal, enter:

composer require jmichaelward/set-post-thumbs

Assuming you're already using Composer's class autoloader in your WordPress project, then you can then call the function JMichaelWard\SetPostThumbs\init_thumbnail_command() inside an mu-plugins PHP file, an active plugin, or your theme's functions.php file.

Alternate

Alternately, you can clone down this repository. Then, require the set-post-thumbs.php file relative to your file path. This will make the JMichaelWard\SetPostThumbs\init_thumbnail_command() method available to your project.

Usage

Once active, this package registers a thumbnail command with WP-CLI. A brief synopsis of the available subcommands is below. Please run wp help thumbnail to see the full set of options.

wp thumbnail set [--all] [--amount=<amount>] [--post_type=<post_type>]

Without options, this will attempt to set a post thumbnail on the first 500 posts WordPress can find without a featured image. You may request that this processes on all posts by passing the --all flag, or a specified amount of posts by using --amount=<amount>. Posts are the default post type, but you can specify other post types with the --post_type flag.

wp thumbnail show <unset|multiple> [--post_type=<post_type>]

Passing unset will return a list of post IDs that were processed, but which the command was unable to set a featured image.

Passing multiple will return a list of post IDs that were processed and where a featured image was set, but where the posts contained multiple images in the content.

As before, passing the --post_type flag will query the selected post type.

wp thumbnail cleanup

This command queries all posts which have metadata generated by the above commands, then subsequently removes that metadata. I recommend running this command after assigning your featured images with this tool to clear out the unnecessary metadata, as it's primarily used for reporting with the wp thumbnail show command.

Extending the command

The ThumbnailCommand class located in the src/ directory makes the above commands available to you. By default, the thumbnail command looks for images within your post content and assigns the first one it finds as the post's featured image. However, your site may have image data located in other places that you may wish to use to set as a featured image. The ThumbnailCommand class is designed to be extended so that you can create your own command from this scenario. The maybe_set_featured_image method should be your primary starting point for extension.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.