based on this example
add_filter( 'facetwp_builder_item_value', function( $value, $item ) {
if ( 'post_excerpt' == $item['source'] ) {
$value = substr( $value, 0, 120 );
}
return $value;
}, 10, 2 );
I would like to trim post_title to a maximum limit of 120 with the (...) at the end.
This objective was not achieved with my modification and I failed several times with several combinations
// my actual attemp to trim post_title item
add_filter( 'facetwp_builder_item_value', function( $value, $item ) {
if ( 'post_title' == $item['source'] ) {
$raw_value = $item['source'];
if ( 120 < strlen( $raw_value ) ) {
$value['source'] = substr( $raw_value, 0, 120 ) . "...";
}
}
return $value;
}, 10, 2 );
following the logic of the things based on this exemple mentioned here
// Add the following to your theme's functions.php
add_filter( 'facetwp_index_row', function( $params, $class ) {
if ( 'aufsichtsbehoerden' == $params['facet_name'] ) {
$raw_value = $params['facet_value'];
if ( 50 < strlen( $raw_value ) ) {
$params['facet_value'] = substr( $raw_value, 0, 50 ); // cut off some of the value
}
}
return $params;
}, 10, 2 );
Experts in PHP and use filters are requested to join me to tell me what I missed as a modification ..
Nest your if statements, and add some way of tracking the bug.
add_filter( 'facetwp_builder_item_value', function( $value, $item ) {
if ( 'post_title' == $item['source']) {
$maxLength = 120;
echo "POST TITLE == ITEM SOURCE";
if (strlen($value) > $maxLength) {
echo "POST TITLE EXCEEDED MAX LENGTH";
$value = substr( $value, 0, $maxLength) . '…';
}
}
return $value;
}, 10, 2 );
I have added two echo functions to allow you to understand where and when the conditions are met.
Your first if is doing nothing in the first code. Notice that there are nothing between its brackets { }.
Your second code is combining both conditions correctly. If it doesn't work, 'post_title' is probably not equal to $item['source'].
Related
the code:
add_filter( 'the_title', 'shorten_woo_product_title', 10, 2 );
function shorten_woo_product_title( $title, $id ) {
if ( ! is_singular( array( 'product' ) ) && get_post_type( $id ) === 'product' && strlen( $title ) > 30 ) {
return substr( $title, 0, 30) . '…'; // change last number to the number of characters you want
} else {
return $title;
}
}
I know I should add wp_is_mobile() somewhere but don't know where should I add it exactly.
If you want to use wp_is_mobile() then it just returns a boolean, so you could use it anywhere that wraps the output:
add_filter( 'the_title', 'shorten_woo_product_title', 10, 2 );
function shorten_woo_product_title( $title, $id ) {
if ( wp_is_mobile() ) {
if ( ! is_singular( array( 'product' ) ) && get_post_type( $id ) === 'product' && strlen( $title ) > 30 ) {
return substr( $title, 0, 30) . '…'; // change last number to the number of characters you want
} else {
return $title;
}
}
return $title;
}
But remember that if you use page caching a mobile user may be the one to generate the cache, and thus your cache will include the change and display everywhere. Given that this specific context is WooCommerce and therefore that maybe you're not caching the product pages if you need them to be dynamic somehow, this may work anyway, but #markus-ao's comment above would be a better solution if caching is an issue.
I'm trying to sort posts on the Manage Posts page by word count. I have the column set up and the count is displayed correct, stripped of all tags, html, etc. However, once I sort it, the posts-orderby filter still includes the tags. So if I have one post that is 1000 words without tags, but 1100 with tags, the sorting function views it as bigger that the post that is 1050 without tags and 1060 with tags. Is there any way to apply all of the code stripping used in the "//Strips content and counts words" section to the final orderby logic?
// Add Length Column
add_filter('manage_post_posts_columns', function ( $columns )
{
$_columns = [];
foreach( (array) $columns as $key => $label )
{
$_columns[$key] = $label;
if( 'title' === $key )
$_columns['ryu_post_content_length'] = __( 'Length' );
}
return $_columns;
} );
//Strips content and counts words
function fsj_count_content_words( $content ) {
$decode_content = html_entity_decode( $content );
$filter_shortcode = strip_shortcodes( $decode_content );
//removes embedded video and images code
$remove_videos = preg_replace('/<figure class="wp-block-embed is-type-video is-provider-tiktok">(.*?)<\/figure>/s', '', $filter_shortcode);
$remove_images = preg_replace('/<div class="wp-block-chroma-blocks-media-upload none">(.*?)<\/div>/s', '', $filter_shortcode);
//strips the html tags
$strip_tags = wp_strip_all_tags( $remove_images, true );
//counts the words
$count = str_word_count( strip_shortcodes($strip_tags) );
return $count;
}
// Fill Column With Word Counts
add_action( 'manage_post_posts_custom_column', function ( $column_name, $post_id )
{
if ( $column_name == 'ryu_post_content_length')
echo fsj_count_content_words(get_post( $post_id )->post_content);
//echo str_word_count( wp_strip_all_tags( get_post( $post_id )->post_content ) );
}, 10, 2 );
// Make Column Orderable
add_filter( 'manage_edit-post_sortable_columns', function ( $columns )
{
$columns['ryu_post_content_length'] = 'ryu_post_content_length';
return $columns;
} );
// Order Through Proper Filter
add_filter( 'posts_orderby', function( $orderby, WP_Query $q )
{
$_orderby = $q->get( 'orderby' );
$_order = $q->get( 'order' );
if(
is_admin()
&& $q->is_main_query()
&& 'ryu_post_content_length' === $_orderby
&& in_array( strtolower( $_order ), [ 'asc', 'desc' ] )
) {
global $wpdb;
$orderby = " LENGTH( {$wpdb->posts}.post_content ) " . $_order . " ";
}
return $orderby;
}, 10, 2 );
I am currently using this code to insert Ads after the 5th paragraph and it works just fine.
//Insert ads after fifth paragraph
add_filter( 'the_content', 'prefix_insert_post_ads' );
function prefix_insert_post_ads( $content ) {
$ad_code = '<center>AD CODE</center>';
if ( is_single() && ! is_admin() ) {
return prefix_insert_after_paragraph( $ad_code, 5, $content );
}
return $content;
}
// Parent Function
function prefix_insert_after_paragraph( $insertion, $paragraph_id, $content ) {
$closing_p = '</p>';
$paragraphs = explode( $closing_p, $content );
foreach ($paragraphs as $index => $paragraph) {
if ( trim( $paragraph ) ) {
$paragraphs[$index] .= $closing_p;
}
if ( $paragraph_id == $index + 1 ) {
$paragraphs[$index] .= $insertion;
}
}
return implode( '', $paragraphs );
}
But, I would like the ads to be repeated after every 5 paragraphs, not just once. What do I do?
Instead of
if ( $paragraph_id == $index + 1 ) {
you can use
if ( $index % $paragraph_id === 0 ) {
that should do it.
I am using WooCommerce Customer/Order CSV Export and have a snippet in my functions file (obtained from WooCommerce.
function sv_wc_csv_export_reorder_columns( $column_headers ) {
// remove order total from the original set of column headers, otherwise it will be duplicated
unset( $column_headers['column_1'] );
unset( $column_headers['delivery_date'] );
$new_column_headers = array();
foreach ( $column_headers as $column_key => $column_name ) {
$new_column_headers[ $column_key ] = $column_name;
if ( 'shipping_company' == $column_key ) {
// add order total immediately after order_number
$new_column_headers['column_1'] = 'Contact Name';
}
}
return $new_column_headers;
}
add_filter( 'wc_customer_order_csv_export_order_headers', 'sv_wc_csv_export_reorder_columns' );
The problem as is with the code, it adds column_1 directly after shipping_company (the first field) - how can I set it so that it appears before this column or set it as the first column?
You need simply the following to set or add 'column_1' as first column:
add_filter( 'wc_customer_order_csv_export_order_headers', 'sv_wc_csv_export_reorder_columns', 10, 2 );
function sv_wc_csv_export_reorder_columns( $column_headers, $csv_generator ) {
// Save "column_1" in a new array
$column1 = array('column_1' => __("Contact Name") );
// Remove "column_1" to be reordered
unset( $column_headers['column_1'] );
// Remove unnecessary "delivery_date"
unset( $column_headers['delivery_date'] );
// Set "column_1" as first column merging arrays
return $column1 + $column_headers;
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.
Try this code
function array_insert_after( array $array, $key, array $new ) {
$keys = array_keys( $array );
$index = array_search( $key, $keys );
$pos = false === $index ? count( $array ) : $index + 1;
return array_merge( array_slice( $array, 0, $pos ), $new, array_slice( $array, $pos ) );
}
function sv_wc_csv_export_reorder_columns( $column_headers ) {
// remove order total from the original set of column headers, otherwise it will be duplicated
unset( $column_headers['column_1'] );
unset( $column_headers['delivery_date'] );
$new_column_headers = array();
$new_column_headers['column_1'] = 'Contact Name';
$column_headers = array_insert_after($column_headers, 'shipping_company', $new_column_headers);
return $column_headers;
}
add_filter( 'wc_customer_order_csv_export_order_headers', 'sv_wc_csv_export_reorder_columns' );
I'm trying to implement a text field validation for field name VoucherNumber that requires the code to be in a certain pattern which is 'WWV-'followed by 4 numbers.
I was successfully able to implement this on google docs using the following expression ^[W]WV-[0-9][0-9][0-9][0-9].
I researched through various answers and attempted to add this code in functions.php but it didn't work. It would just show that the form is being sent (spinning wheel) but it would not be sent even after 5 minutes.
add_filter( 'wpcf7_validate_text*', 'validate_voucher_number', 20, 2 );
function validate_voucher_number( $result, $tag ) {
$tag = new WPCF7_FormTag( $tag );
if ( 'VoucherNumber' == $tag->name ) {
$VoucherNumber = isset( $_POST['VoucherNumber'] ) ? trim( $_POST['VoucherNumber'] ) : '';
if ( ! preg_match ( "^[W]WV-[0-9][0-9][0-9][0-9]" , $VoucherNumber) ){
$result->invalidate( $tag, "Voucher number is invalid" );
}
}
return $result;
}
Just make sure that your field name is VoucherNumber and try this code :
add_filter( 'wpcf7_validate_text', 'vouchervalidation', 20, 2 );
function vouchervalidation( $result, $tag ) {
if ( 'VoucherNumber' == $tag->name ) {
$VoucherNumber = isset( $_POST['VoucherNumber'] ) ? trim( $_POST['VoucherNumber'] ) : '';
if ( ! preg_match ( "^[W]WV-[0-9][0-9][0-9][0-9]" , $VoucherNumber) ){
$result->invalidate( $tag, "Voucher number is invalid" );
}
}
return $result;
}