Sharing Image provides developers with a large number of hooks that can be used to change the behavior of
the plugin.
Put the hook code in functions.php file of your theme or in your plugin.
Use this action to add something before the settings widget.
If you want to extend js object inside settings page, use the sharing_image_settings_object filter.
Show me an example
function my_settings_before() {
echo 'This text will be displayed in the settings page';
}
add_action( 'sharing_image_settings', 'my_settings_before' );
WordPress filters have the same idea as actions, but the main difference is that filters are used to modify
variables. Unlike actions, filters code must return a value, which is the modified copy of the original value.
You can filter public poster url, width and height for current Object (Post or Term taxonomy) ID with this hook.
Show me an example
/**
* @param array|false $image List of image data, or boolean false if no image is available.
* @param int $object_id Post ID or Taxonomy term ID.
*/
function my_poster_src( $poster, $object_id ) {
$poster = array(
'https://example.org/image.png',
1200,
630,
);
return $poster;
}
add_filter( 'sharing_image_poster_src', 'my_poster_src', 10, 2 );
The filter is used to update automatically initialized plugin classes.
You can remove or add custom class on plugin load.
Show me an example
/**
* @param array $classes List of classes.
*/
function my_update_classes( $classes ) {
unset( $classes['Sharing_Image\Meta'] );
return $classes;
}
// This filter must execute before the `plugins_loaded` event.
add_filter( 'sharing_image_plugin_classes', 'my_update_classes' );
Prepare template before creating poster with this filter.
Used to update fieldset texts and background image.
Show me an example
/**
* @param array $template List of template data.
* @param array $fieldset Fieldset data from picker.
* @param string $index Template index from editor.
* @param int $screen_id Post or term admin page id. Will be 0 if not set.
* @param string $context Screen ID context field. Can be settings, post or term.
*/
function my_prepare_template( $template, $fieldset, $index, $screen_id, $context ) {
if ( 'post' === $context && 1 === $screen_id ) {
$template['fill'] = '#fff';
}
return $template;
}
add_filter( 'sharing_image_prepare_template', 'my_prepare_template', 10, 5 );
You can filter generated poster file path and url.
By default used unique file name and upload directory from settings.
Use sharing_image_upload_dir hook to update upload directory.
Show me an example
/**
* @param array $file Server file path and url to image.
* @param string $name Unique file name.
*/
function my_get_upload_file( $file, $name ) {
$custom = 'custom.png';
// Replace unique file name with custom one in image url and path.
foreach ( $file as &$value ) {
$value = str_replace( $name, $custom, $value );
}
return $file;
}
add_filter( 'sharing_image_get_upload_file', 'my_get_upload_file', 10, 2 );
If you don't have a Premium license, but you need to access all the features of the plugin for development purposes,
you can use it in special mode. To do this, simply set the filter to true.
Show me an example
/**
* @param bool Current development state. Disabled by default.
*/
function my_develop( $is_develop ) {
return true;
}
add_filter( 'sharing_image_develop', 'my_develop' );
Use this filter to update license data right after receiving it from database options.
Please be careful with this filter and do not use it to violate the plugin licensing policy.
Show me an example
/**
* @param array List of plugin license settings.
*/
function my_get_license( $license ) {
return $license;
}
add_filter( 'sharing_image_get_license', 'my_get_license' );
You can update the directory to uploaded posters with this filter.
Note that the parameter is an array with the local path to the directory on the server and its external url.
Show me an example
/**
* @param array $dir Path and url to upload directory.
*/
function my_upload_dir( $dir ) {
$uploads = $dir;
return array( $uploads['path'], $uploads['url'] );
}
add_filter( 'sharing_image_upload_dir', 'my_upload_dir' );
You can rewrite generated poster quality with this filter.
By default it is taken from plugin settings.
Works only for JPEG posters. Set 0 for low resoulation posters and 100 for best quality.
Use this filter to hide OpenGraph and Twitter meta tags from wp_head section.
Useful if you want to output these tags manually or disable them completely.
Show me an example
/**
* @param bool $hide_header Set true to hide poster meta.
*/
function my_default_hide_meta( $hide_meta ) {
return true;
}
add_filter( 'sharing_image_hide_meta', 'my_default_hide_meta' );
Use this filter to update post types where the metabox is displayed.
By default, these are all public types of posts, excluding attachments.
Show me an example
/**
* @param array $post_types Array of post types for which the metabox is displayed.
*/
function my_metabox_post_types( $post_types ) {
unset( $post_types['pages'] );
return $post_types;
}
add_filter( 'sharing_image_metabox_post_types', 'my_metabox_post_types' );
Filter template unique index on creation. It may be useful when you want to override template index names.
Use in conjunction with the sharing_image_valid_unique_index filter.
This filter allows you to change the template index validation. It may be useful when you want to override template index names.
Use in conjunction with the sharing_image_unique_index filter.
Use this filter to update the fieldset during poster auto-generation.
This can be very useful if you want to ignore widget settings and provide your own values under certain conditions.
Additionally, use this filter if you have removed the widget from the post editing page but still want to provide your own values for the poster.
This filter allows you to customize the attachment that will be added during poster generation if such a setting is used.
Return an empty value if you do not want to save the attachment.
For example, you may choose not to save the attachment only for auto-generated posts.
Show me an example
/**
* @since 3.0
*
* @param string $attachment Attachment data.
* @param string $path Path to poster image.
* @param string $screen_id Post or taxonomy screen ID.
* @param string $context Widget context. For example: metabox or autogenerate.
*/
function my_attachment_data( $attachment, $path, $screen_id, $context ) {
if ( autogenerate === $context ) {
$attachment = null;
}
return $attachment;
}
add_filter( 'sharing_image_attachment_data', 'my_attachment_data', 10, 4 );
This filter allows you to add or override classes for snippets.
Snippets are used to interact with third-party plugins that add their own social media posters to the page.
Show me an example
/**
* @since 3.0
*
* @param array $snippets A key-value array with class name key and path to the file as value.
*/
function my_image_snippets( $snippets ) {
unset( $snippets['Sharing_Image\Snippets\YoastSeo'] )
return $snippets;
}
add_filter( 'sharing_image_snippets', 'my_image_snippets', 10 );
Use this filter to hide OpenGraph and Twitter meta tags from wp_head section.
Useful if you want to output these tags manually or disable them completely.
Show me an example
/**
* @param bool $hide_header Set true to hide poster meta.
*/
function my_default_hide_meta( $hide_meta ) {
return true;
}
add_filter( 'sharing_image_hide_meta', 'my_default_hide_meta' );