Summary: Learning about different WordPress functions.
media_sideload_image()
The media_sideload_image()
function in WordPress allows us to download an image from a URL and add it to the WordPress Media Library. This function is typically used to “sideload” images from external URLs into WordPress, similar to uploading images manually.
media_sideload_image( string $file, int $post_id, string $desc = null, string $return = 'html' )
$file
(string) – The URL of the file we want to sideload (usually an image URL).$post_id
(int) – The ID of the post that we want to attach the sideloaded image to. This can be a post, page, or any custom post type.$desc
(string) – (Optional) The description of the image, which will be stored in the image’s attachment metadata (i.e., the alt text).$return
(string) – (Optional) Specifies what the function should return.
'html'
: (default) Returns the HTML<img>
tag.'src'
: Returns the URL of the image.'id'
: Returns the attachment ID of the image.
// URL of the image to sideload
$image_url = 'https://example.com/path-to-image.jpg';
// Post ID to attach the image to
$post_id = 123;
// Optional description for the image
$description = 'Sample Image Description';
// Sideload the image
$image_id = media_sideload_image( $image_url, $post_id, $description, 'id' );
// Check for success or failure
if ( !is_wp_error( $image_id ) ) {
// Success: Do something with the image ID
set_post_thumbnail( $post_id, $image_id ); // Set as the featured image for the post
} else {
// Error handling
echo 'Error sideloading image: ' . $image_id->get_error_message();
}
- The function handles downloading the image and saving it as an attachment in the Media Library.
- The
$return
parameter gives flexibility in how we use the result (HTML, image URL, or attachment ID). - We can use the returned attachment ID to set it as a post’s featured image with
set_post_thumbnail()
.
date_i18n()
The date_i18n()
function is similar to PHP’s date()
function but is localized, meaning it will output dates in a format that respects the WordPress site’s locale and language settings. It also accounts for time zone adjustments.
- Localized version of
date()
. - Takes time zone settings into account.
- Returns a date string formatted according to the current locale of the site, ensuring that dates are displayed appropriately for the language and time zone.
$date_format = get_option( 'date_format' );
$release_date = date_i18n( $date_format ? $date_format : 'd-m-Y', $release_date );
update_post_term_cache
This parameter controls whether the taxonomy terms like categories or tags associated with the queried posts should be automatically cached when fetching posts.
true
(default): WordPress will fetch and cache taxonomy terms for the posts in the query. This is generally useful if we need access to the post’s terms later in our code, as it avoids the need for extra database queries.false
: WordPress will not fetch or cache the taxonomy terms, meaning it skips this extra step. This can improve performance when the terms are not needed.
$args = array(
'post_type' => 'rt-movie',
'posts_per_page' => 10,
'update_post_term_cache' => false, // Disable term caching
);
$query = new WP_Query( $args );
Custom User Role
The add_role()
function in WordPress allows us to create a custom user role with specific capabilities. This function is often used when we need to add a role for users with a specific set of permissions that don’t fit into the default roles like Administrator, Editor, Author, Contributor, or Subscriber.
add_role( string $role, string $display_name, array $capabilities = array() )
$role
(string) – The unique name (slug) for the custom role. It should be lowercase and contain no spaces or special characters, as this will be used internally in WordPress (e.g.,'movie_manager'
).$display_name
(string) – The human-readable name of the role that will be displayed in the WordPress dashboard (e.g.,'Movie Manager'
).$capabilities
(array) – An associative array of capabilities for the role. Each key in the array represents a capability, and the value (true
orfalse
) indicates whether the role has that capability.
function add_movie_manager_role() {
add_role(
'movie_manager',
'Movie Manager',
array(
'read' => true,
'edit_rt-movie' => true,
'edit_rt-movies' => true,
'edit_others_rt-movies' => true,
'publish_rt-movies' => true,
'delete_rt-movie' => true,
'delete_rt-movies' => true,
'read_private_rt-movies' => true,
)
);
}
add_action( 'init', 'add_movie_manager_role' );
Rewrite Rules
In WordPress functions like add_rewrite_tag()
and add_permastruct()
are used to create custom URL structures pretty permalinks and query variables. These functions help build more readable and user-friendly URLs for custom content, such as custom post types, taxonomies, and query variables. They also allow for greater flexibility in handling requests.
add_rewrite_tag()
The add_rewrite_tag()
function allows us to create a custom rewrite tag, which essentially serves as a placeholder in the URL structure that can be mapped to query variables.
add_rewrite_tag( string $tag, string $regex, string $query = '' )
$tag
(string) – The rewrite tag, which should start with a%
and end with a%
(e.g.,%movie_id%
).$regex
(string) – The regular expression to match for this rewrite tag in URLs.$query
(string) – The default query variable that this rewrite tag will map to.
function add_movie_year_rewrite_tag() {
add_rewrite_tag( '%movie_year%', '([0-9]{4})', 'movie_year=' );
}
add_action( 'init', 'add_movie_year_rewrite_tag' );
%movie_year%
is the rewrite tag.([0-9]{4})
is the regex pattern to match a 4-digit year (e.g., 2024).'movie_year='
tells WordPress to create a query variablemovie_year
when this tag appears in the URL.
add_permastruct()
The add_permastruct()
function defines a custom permalink structure for a specific type of content. It allows us to create custom URL patterns for post types, taxonomies, and other custom structures.
add_permastruct( string $name, string $struct, array|string $args = array() )
$name
(string) – A name for the permalink structure (e.g.,'movie'
).$struct
(string) – The permalink structure itself. This can include rewrite tags like%movie_id%
,%postname%
, or custom rewrite tags like%movie_year%
.$args
(array|string) – Additional arguments to control behavior like whether it’s for a hierarchical post type, and the pagination base.
function add_movie_permastruct() {
add_permastruct(
'movie',
'/movies/%movie_year%/%postname%',
array(
'with_front' => false,
'ep_mask' => EP_PERMALINK
)
);
}
add_action( 'init', 'add_movie_permastruct' );
This creates a custom permalink structure for the rt-movie
post type where:
/movies/%movie_year%/%postname%
is the URL pattern, which includes the movie’s release year from the rewrite tag%movie_year%
, followed by the movie name (%postname%
).- The permalink will look like:
example.com/movies/2024/the-movie-name
.
Leave a Reply