Skip to content

Instantly share code, notes, and snippets.

View wp-seopress's full-sized avatar
😊
Coding SEOPress, best WordPress SEO plugin

Benjamin Denis wp-seopress

😊
Coding SEOPress, best WordPress SEO plugin
View GitHub Profile
@wp-seopress
wp-seopress / filter-the-prompt-sent-to-openai-to-generate-alt-text-for-images.php
Created October 25, 2023 08:32
Filter the prompt sent to OpenAI to generate alt text for images
function sp_ai_openai_alt_text($prompt_alt_text, $post_id) {
$language = 'English';
$image_src = wp_get_attachment_image_src($post_id, 'full');
$prompt_alt_text = sprintf(__('Write in less than 10 words an alternative text to improve accessibility and SEO, in this language %1$s, for this image URL: %2$s.', 'wp-seopress-pro'), esc_attr($language), esc_html($image_src[0]));
return $prompt_alt_text;
}
add_filter( 'seopress_ai_openai_alt_text', 'sp_ai_openai_alt_text', 10, 2 );
@wp-seopress
wp-seopress / filter-google-analytics-ecommerce-view-details-item-event.php
Created September 14, 2023 14:23
Filter Google Analytics Ecommerce view details item event
add_filter('seopress_gtag_ec_single_view_details_ev', 'sp_gtag_ec_single_view_details_ev');
function sp_gtag_ec_single_view_details_ev($js) {
$js = 'gtag("event", "view_item", {
currency: "USD",
value: 7.77,
items: [
{
item_id: "SKU_12345",
item_name: "Stan and Friends Tee",
affiliation: "Google Merchandise Store",
@wp-seopress
wp-seopress / parent-terms-title.php
Last active September 4, 2023 12:51
Dynamic variable for SEOPress: get term parents list of the current term taxonomy
function sp_titles_template_variables_array($array) {
$array[] = '%%parent_terms_title%%';
return $array;
}
add_filter('seopress_titles_template_variables_array', 'sp_titles_template_variables_array');
function sp_titles_template_replace_array($array) {
$queried_object = get_queried_object();
$term_id = $queried_object->term_id;
@wp-seopress
wp-seopress / display-posts-of-custom-post-type-hierarchically-by-custom-taxonomy.php
Last active August 23, 2023 13:16
Display posts of custom post type hierarchically by custom taxonomy
add_filter('seopress_sitemaps_html_hierarchical_terms_query', 'sp_sitemaps_html_hierarchical_terms_query', 10, 2);
function sp_sitemaps_html_hierarchical_terms_query($cpt_key, $args_terms_query) {
if ($cpt_key === 'testimonials') {//replace 'testimonials' with your own post type key
$cats = get_terms('type', $args_terms_query);
return $cats;
}
return;
}
add_filter('seopress_sitemaps_html_hierarchical_tax_query', 'sp_sitemaps_html_hierarchical_tax_query', 10, 3);
@wp-seopress
wp-seopress / filter-primary-categories-product-categories-list.php
Created August 22, 2023 09:17
Filter primary categories / product categories list
function sp_primary_category_list($cats) {
//default
//$cats = get_categories();
// global $typenow;
// global $post;
// if ('product' == $typenow) {
// $cats = get_the_terms($post_id, 'product_cat');
// }
@wp-seopress
wp-seopress / filter-product-category-slug-used-in-html-sitemap.php
Created August 8, 2023 14:12
Filter product category slug used in HTML sitemap
add_filter('seopress_sitemaps_html_product_cat_slug', 'sp_sitemaps_html_product_cat_slug');
function sp_sitemaps_html_product_cat_slug($product_cat_slug) {
$product_cat_slug = 'product_cat'; //default
return $product_cat_slug;
}
@wp-seopress
wp-seopress / exclude-out-of-stock-woocommerce-products-from-the-xml-sitemap.php
Last active July 21, 2023 08:16
Exclude out of stock WooCommerce products from the XML sitemap
function sp_sitemaps_single_query($args, $cpt_key) {
if ( $cpt_key == 'product' ) {
$args['tax_query'][] = array(
'taxonomy' => 'product_visibility',
'field' => 'slug',
'terms' => ['exclude-from-catalog', 'outofstock'],
'operator' => 'NOT IN',
);
}
return $args;
@wp-seopress
wp-seopress / filter-rss-post-thumbnail-size.php
Last active July 7, 2023 10:30
Filter RSS post thumbnail size
add_filter('seopress_rss_post_thumb_size', 'sp_rss_post_thumb_size');
function sp_rss_post_thumb_size($size) {
//Will be used with wp_get_attachment_image_src() https://developer.wordpress.org/reference/functions/wp_get_attachment_image_src/
$size = 'thumbnail'; //Accepts any registered image size name (thumbnail, medium, large, full or custom size name), or an array of width and height values in pixels (in that order).
return $size;
}
@wp-seopress
wp-seopress / remove-last-crumb-singular-page-post.php
Created June 12, 2023 16:10
Remove last crumb on singular page/post
//Remove last crumb on singular page/post
add_filter('seopress_pro_breadcrumbs_crumbs', 'sp_pro_breadcrumbs_crumbs');
function sp_pro_breadcrumbs_crumbs($crumbs) {
if (is_singular()) {
add_filter('seopress_pro_breadcrumbs_last_item_linkable', '__return_true');
end($crumbs);
$lastKey = key($crumbs);
@wp-seopress
wp-seopress / filter-_ucf_your_user_meta-dynamic-variable.php
Last active May 29, 2023 15:48
Filter %%_ucf_your_user_meta%% dynamic variable
add_filter('seopress_titles_user_meta', 'sp_titles_user_meta', 10, 2);
function sp_titles_user_meta($user_meta_value, $user_meta_key) {
//do your stuff
//$user_meta_value = esc_attr(get_user_meta(get_current_user_id(), $user_meta_key, true));
return $user_meta_value;
}