This module will allow users to define a subset of posts to be displayed in a
theme-designated featured content area.
This feature will only be activated for themes that declare that they support
it. This can be done by adding code similar to the following during the
"after_setup_theme" action:
For maximum compatibility with different methods of posting users will
designate a featured post tag to associate posts with. Since this tag now has
special meaning beyond that of a normal tags, users will have the ability to
hide it from the front-end of their site.
The maximum number of posts that a Featured Content area can contain. We
define a default value here but themes can override this by defining a
"max_posts" entry in the second parameter passed in the call to
add_theme_support( 'featured-content' ).
$post_types
$post_types
The registered post types supported by Featured Content. Themes can add
Featured Content support for registered post types by defining a
'post_types' argument (string|array) in the call to
add_theme_support( 'featured-content' ).
Methods
setup()
setup()
Instantiate.
All custom functionality will be hooked into the "init" action.
init()
init()
Conditionally hook into WordPress.
Themes must declare that they support this module by adding
add_theme_support( 'featured-content' ); during after_setup_theme.
If no theme support is found there is no need to hook into WordPress. We'll
just return early instead.
wp_loaded()
wp_loaded()
Hide "featured" tag from the front-end.
Has to run on wp_loaded so that the preview filters of the customizer
have a chance to alter the value.
get_featured_posts()
get_featured_posts() : array
Get featured posts
This method is not intended to be called directly. Theme developers should
place a filter directly in their theme and then pass its name as a value of
the "filter" key in the array passed as the $args parameter during the call
to: add_theme_support( 'featured-content', $args ).
Returns
array
get_featured_post_ids()
get_featured_post_ids() : array
Get featured post IDs
This function will return the an array containing the post IDs of all
featured posts.
Sets the "featured_content_ids" transient.
Returns
array
—
Array of post IDs.
delete_transient()
delete_transient()
Delete Transient.
Hooks in the "save_post" action.
pre_get_posts()
pre_get_posts(\WP_Query $query) : \WP_Query
Exclude featured posts from the blog query when the blog is the front-page.
Filter the home page posts, and remove any featured post ID's from it.
Hooked onto the 'pre_get_posts' action, this changes the parameters of the
query before it gets any posts.
Parameters
\WP_Query
$query
Returns
\WP_Query
—
Possibly modified WP_Query
delete_post_tag()
delete_post_tag(int $tag_id) : void
Reset tag option when the saved tag is deleted.
It's important to mention that the transient needs to be deleted, too.
While it may not be obvious by looking at the function alone, the transient
is deleted by Featured_Content::validate_settings().
Renders all form fields on the Settings -> Reading screen.
get_setting()
get_setting(string $key) : mixed
Get settings
Get all settings recognized by this module. This function will return all
settings whether or not they have been stored in the database yet. This
ensures that all keys are available at all times.
In the event that you only require one setting, you may pass its name as the
first parameter to the function and only that value will be returned.
Parameters
string
$key
The key of a recognized setting.
Returns
mixed
—
Array of all settings by default. A single value if passed as first parameter.
validate_settings()
validate_settings(array $input) : array
Validate settings
Make sure that all user supplied content is in an expected format before
saving to the database. This function will also delete the transient set in
Featured_Content::get_featured_content().
Parameters
array
$input
Returns
array
—
$output
switch_theme()
switch_theme() : void
Removes the quantity setting from the options array.