Add different WordPress excerpt formats to different templates - wordpress

I added the following code to my functions.php file in WordPress 6.1.1 to display excerpts.
function new_excerpt_length($length) {
return 100;
}
add_filter('excerpt_length', 'new_excerpt_length');
function new_excerpt_more($more) {
return '...';
}
add_filter('excerpt_more', 'new_excerpt_more');
...but I also have a use case to show the full excerpt without a read more link.
On page template 1 I add the below code to display the excerpt:
<?php echo the_excerpt(); ?>
...and it displays the excerpt as per the functions.php file but how do I create a 2nd excerpt without the read more link and apply it to page template 2?
Is there a parameter I can use within the_excerpt(parameter); or can I use something like wp_trim_excerpt https://developer.wordpress.org/reference/functions/wp_trim_excerpt/ maybe?
I came across the below code that is supposed to do what I want
function wpex_get_excerpt( $args = array() ) {
// Default arguments.
$defaults = array(
'post' => '',
'length' => 40,
'readmore' => false,
'readmore_text' => esc_html__( 'read more', 'text-domain' ),
'readmore_after' => '',
'custom_excerpts' => true,
'disable_more' => false,
);
// Apply filters to allow child themes mods.
$args = apply_filters( 'wpex_excerpt_defaults', $defaults );
// Parse arguments, takes the function arguments and combines them with the defaults.
$args = wp_parse_args( $args, $defaults );
// Apply filters to allow child themes mods.
$args = apply_filters( 'wpex_excerpt_args', $args );
// Extract arguments to make it easier to use below.
extract( $args );
// Get the current post.
$post = get_post( $post );
// Get the current post id.
$post_id = $post->ID;
// Check for custom excerpts.
if ( $custom_excerpts && has_excerpt( $post_id ) ) {
$output = $post->post_excerpt;
}
// No custom excerpt...so lets generate one.
else {
// Create the readmore link.
$readmore_link = '' . $readmore_text . $readmore_after . '';
// Check for more tag and return content if it exists.
if ( ! $disable_more && strpos( $post->post_content, '<!--more-->' ) ) {
$output = apply_filters( 'the_content', get_the_content( $readmore_text . $readmore_after ) );
}
// No more tag defined so generate excerpt using wp_trim_words.
else {
// Generate an excerpt from the post content.
$output = wp_trim_words( strip_shortcodes( $post->post_content ), $length );
// Add the readmore text to the excerpt if enabled.
if ( $readmore ) {
$output .= apply_filters( 'wpex_readmore_link', $readmore_link );
}
}
}
// Apply filters and return the excerpt.
return apply_filters( 'wpex_excerpt', $output );
}
Output using:
<?php echo wpex_get_excerpt ( $defaults = array(
'length' => 40,
'readmore' => true,
'readmore_text' => esc_html__( 'read more', 'wpex-boutique' ),
'custom_excerpts' => true,
) ); ?>
...but doesn't seem to workas intended. Outputs the excerpt with no link but the args don't see to work when changed. Would be perfect for my use otherwise. Maybe someone sees how to fix this code?
Thanks

Related

Wordpress improve shortcode with more parameters

when you are a new designer and start working with several content editors in wordpress, the first thing you realize is that it is not easy to find the functions you want exactly.
plugin editors can't think of everything.. and especially extract information to display them everywhere in the desired and consistent manner.
that's why wordpress shortcodes exist. but you have to master them.. first! (which is precisely my handicap currently)
Grab your cup of coffee ☕ and admire the detailed visual of today's issue
It will be very short and precise,
before starting look at what taxonomies we have in our post..
my current code allows me to do figure 1, as you can see it quickly becomes limited when you try to be .. more creative
add_shortcode( 'parent-child', 'taxonomy_hierarchy' );
function taxonomy_hierarchy( $atts ){
// Don't extract, rather parse shortcode params with defaults.
$atts = shortcode_atts( array(
'link' => 'true',
'taxonomy' => 'property_city'
), $atts, 'parent-child' );
// Check the $atts parameters and their typecasting.
//var_dump( $atts );
global $post;
$terms = wp_get_post_terms( $post->ID, $atts['taxonomy'] );
/* You can pass conditions here to override
* $link based on certain conditions. If it's
* a single post, current user is editor, etc.
*/
ob_start();
foreach( $terms as $term ){
if( $term->parent != 0 ){
$parent_term = get_term( $term->parent, $taxonomy );
if ($atts['link'] !== 'false') {
printf( '%s, ', esc_url( get_term_link($parent_term) ), $parent_term->name );
} else {
echo $parent_term->name . ', ';
}
}
if ($atts['link'] !== 'false') {
printf( '%s', esc_url( get_term_link($term) ), $term->name );
} else {
echo $term->name;
}
}
return ob_get_clean();
}
in this figure 1 we don't need to display "residential" and "rental" it doesn't make sense it's confusing. we don't have control over this.
but..What if I want to test and accomplish figure 2, figure 3 and figure 4
You : but there are already shortcodes what is it???
yes, don't worry, I took care to simulate a logic of things inside shortcodes, maybe that's not exactly how it's going to be. or how things work ..
I prefer to remain cautious and wait for your opinion or solution.
if logic allows, we need to separate the parent and childs values ​​and paste them back as we want
like a puzzle
Something Like 👀
↑[taxonomy-parent = "name_of_my_taxonomy"]
↓[taxonomy-child = "name_of_my_taxonomy"]
or simply reverse in some cases
←[child-parent] (manhattan, new york)
→[parend-child] (new york, manhattan)
note that I used [acf field "name field here"] to invoke a price value, if it is possible to fit it inside our combination or leave it like that?
final word: you can tell I spent some time working out and thinking about this, if only coding was that easy for me I would have done even better. for now that's all I need more flexibility to display information in different ways.
You could consider using an array() instead to store the $parent if it exists, and then the $child which should exist. This would give you the benefit of being able to use array_reverse() to flip the order of the terms based on a parameter saying whether you want the parent or the child first. This gives you the added benefit of being able to convert the array to a string with the implode() function, which you can pass a $glue argument to, to determine whether you want them separated by a space, comma + space, hyphen, slash, etc.
add_shortcode( 'parent-child', 'taxonomy_hierarchy' );
function taxonomy_hierarchy( $atts ){
// Don't extract, rather parse shortcode params with defaults.
$atts = shortcode_atts( array(
'link' => 'true',
'glue' => ', ',
'taxonomy' => 'property_city',
'first' => 'parent',
), $atts, 'parent-child' );
// Check the $atts parameters and their typecasting.
//var_dump( $atts );
global $post;
$terms = wp_get_post_terms( $post->ID, $atts['taxonomy'] );
/* You can pass conditions here to override
* $link based on certain conditions. If it's
* a single post, current user is editor, etc.
*/
$array = array();
foreach( $terms as $term ){
if( $term->parent != 0 ){
$parent_term = get_term( $term->parent, $taxonomy );
if ($atts['link'] !== 'false') {
$array[] = sprintf( '%s', esc_url( get_term_link($parent_term) ), $parent_term->name );
} else {
$array[] = $parent_term->name;
}
}
if( $atts['link'] !== 'false' ){
$array = sprintf( '%s', esc_url( get_term_link($term) ), $term->name );
} else {
$array = $term->name;
}
}
if( $atts['first'] != 'parent' ){
$array = array_reverse( $array );
}
return implode($glue, $array);
}
Doing this should give you the ability to put out shortcodes like:
[parent-child first="child" glue=" - "] // Child - Parent
[parent-child glue="/"] // Parent/Child
Documentation & Function Reference
Function
Linked Description
array_reverse()
Return an array with elements in reverse order
implode()
Join array elements with a string
I think I found a solution but in this case it is no longer a shortcode with parameters.
it's become a bunch of shortcodes with a parameters, at least we have more flexibility now.
I simply cut the code into 3 parts
if you are not afraid to see 😰 very long complicated shortcodes. it's a solution that will help.
but I'm not sure if this is the best practice.
output taxonomy parent, child
/*output taxonomy parent, child*/
add_shortcode( 'parent-child', 'taxonomy_hierarchy' );
function taxonomy_hierarchy( $atts ){
$atts = shortcode_atts( array(
'link' => 'true',
'taxonomy' => 'property_city' // my default taxonomy
), $atts, 'parent-child' );
global $post;
$terms = wp_get_post_terms( $post->ID, $atts['taxonomy'] );
ob_start();
foreach( $terms as $term ){
if( $term->parent != 0 ){
$parent_term = get_term( $term->parent, $taxonomy );
if ($atts['link'] !== 'false') {
printf( '%s, ', esc_url( get_term_link($parent_term) ), $parent_term->name );
} else {
echo $parent_term->name . ', ';
}
}
if ($atts['link'] !== 'false') {
printf( '%s', esc_url( get_term_link($term) ), $term->name );
} else {
echo $term->name;
}
}
return ob_get_clean();
}
output only taxonomy parent
/*output only taxonomy parent*/
add_shortcode( 'parent', 'taxonomy_parent' );
function taxonomy_parent( $atts ){
$atts = shortcode_atts( array(
'link' => 'true',
'taxonomy' => 'property_city' // my default taxonomy
), $atts, 'parent' );
global $post;
$terms = wp_get_post_terms( $post->ID, $atts['taxonomy'] );
ob_start();
foreach( $terms as $term ){
if( $term->parent != 0 ){
$parent_term = get_term( $term->parent, $taxonomy );
if ($atts['link'] !== 'false') {
printf( '%s', esc_url( get_term_link($parent_term) ), $parent_term->name );
} else {
echo $parent_term->name;
}
}
}
return ob_get_clean();
}
output only taxonomy child
/*output only taxonomy child*/
add_shortcode( 'child', 'taxonomy_child' );
function taxonomy_child( $atts ){
$atts = shortcode_atts( array(
'link' => 'true',
'taxonomy' => 'property_city' // my default taxonomy
), $atts, 'child' );
global $post;
$terms = wp_get_post_terms( $post->ID, $atts['taxonomy'] );
ob_start();
foreach( $terms as $term ){
if ($atts['link'] !== 'false') {
printf( '%s', esc_url( get_term_link($term) ), $term->name );
} else {
echo $term->name;
}
}
return ob_get_clean();
}

Solution to Excerpt Customisation with new Gutenberg Editor

I have made several customisations to the native Wordpress Excerpt metabox which no longer seem to work with the new Gutenberg editor and I can find no documentation online that guides me to a solution.
Does anyone know how the following code snippets can be amended to work with the new Gutenberg Editor?
This snippet removes the native Excerpt and repositions it at the top of the Editor screen below the Title metabox:
function remove_normal_excerpt() {
remove_meta_box( 'postexcerpt' , 'post' , 'normal' );
}
add_action( 'admin_menu' , 'remove_normal_excerpt' );
function add_new_excerpt_meta( $post_type ) {
$post_types = get_post_types( array ( 'public' => true ) );
$excluded_posttypes = array ();
$post_type = array_diff($post_types, $excluded_posttypes);
add_meta_box(
'postexcerpt',
__( 'Excerpt', '' ),
'post_excerpt_meta_box',
$post_type,
'advanced',
'high', 0, 0
);
}
add_action( 'add_meta_boxes', 'add_new_excerpt_meta' );
This snippet adds an information notice to the Excerpt metabox:
function excerpt_add_notice( $translation, $original ) {
if ( 'Excerpt' == $original ) {
return 'Excerpt';
}else{
$pos = strpos($original, 'Excerpts are optional hand-crafted summaries of
your');
if ($pos !== false) {
return '<h5 class="notice notice info">Always complete your excerpt
sections.</h5>';
}
}
return $translation;
}
add_filter( 'gettext', 'excerpt_add_notice', 10, 2 );
This snippet adds a javascript character count limit to the Excerpt:
function excerpt_count_js(){
global $post;
if ( get_post_type($post) != 'storefront' ) {
echo '<script>jQuery(document).ready(function(){
jQuery("#postexcerpt .handlediv").after("<div
style=\"position:absolute;top:12px;right:34px;color:#666;\"><small>Excerpt
length: </small><span id=\"excerpt_counter\"></span><span style=\"font-
weight:bold; padding-left:7px;\">/ 170</span><small><span style=\"font-
weight:bold; padding-left:7px;\">character(s).</span></small></div>");
jQuery("span#excerpt_counter").text(jQuery("#excerpt").val().length);
jQuery("#excerpt").keyup( function() {
if(jQuery(this).val().length > 170){
jQuery(this).val(jQuery(this).val().substr(0, 170));
}
jQuery("span#excerpt_counter").text(jQuery("#excerpt").val().length);
});
});</script>';
}
}
add_action( 'admin_head-post.php', 'excerpt_count_js');
add_action( 'admin_head-post-new.php', 'excerpt_count_js');
Is it just a case of something simple as the Excerpt metabox identifiers having been changed?

Display custom fields in variations product page WooCommerce

I've created two custom fields in variations with the following code (Thanks Remi Corso):
functions.php
Add Variation Settings
add_action( 'woocommerce_product_after_variable_attributes', 'variation_settings_fields', 10, 3 );
Save Variation Settings
add_action( 'woocommerce_save_product_variation', 'save_variation_settings_fields', 10, 2 );
Create new fields for variations
function variation_settings_fields( $loop, $variation_data, $variation ) {
woocommerce_wp_text_input(
array(
'id' => '_pdf_ficha_tecnica[' . $variation->ID . ']',
'label' => __( 'PDF FICHA TÉCNICA', 'woocommerce' ),
'placeholder' => 'http://',
'desc_tip' => 'true',
'description' => __( 'aqui', 'woocommerce' ),
'value' => get_post_meta( $variation->ID, '_pdf_ficha_tecnica', true )
)
);
woocommerce_wp_text_input(
array(
'id' => '_pdf_ficha_caracteristicas[' . $variation->ID . ']',
'label' => __( 'PDF FICHA CARACTERÍSTICAS', 'woocommerce' ),
'placeholder' => 'http://',
'desc_tip' => 'true',
'description' => __( 'aqui', 'woocommerce' ),
'value' => get_post_meta( $variation->ID, '_pdf_ficha_caracteristicas', true )
)
);
}
Save new fields for variations
function save_variation_settings_fields( $post_id ) {
$text_field = $_POST['_pdf_ficha_tecnica'][ $post_id ];
if( ! empty( $text_field ) ) {
update_post_meta( $post_id, '_pdf_ficha_tecnica', esc_attr( $text_field ) );
}
$text_field = $_POST['_pdf_ficha_caracteristicas'][ $post_id ];
if( ! empty( $text_field ) ) {
update_post_meta( $post_id, '_pdf_ficha_caracteristicas', esc_attr( $text_field ) );
}
}
These custom fields store URLs and will be displayed as links <a href>. I looking to display these fields but I'm having a lot of trouble finding the right solution.
Can anyone guide me?
Should I focus on file "variable.php"?
And the JS?
Or can I render the fields by hooks?
The following code I created works perfectly. I am new to JS and I'm sure can be improved. I hope that will be helpful. To create Custom Fields reads the post of REMI.
Explanation: With "WC_Product Variable" object can display the Custom Fields of product variation,
To display these fields I used jquery, the contents of the span "sku" will be our reference as shown on the product page. This code in the "variations.php" file.
<?php
// With "WC_Product Variable" object I get the Custom Fields variations.
$product_id = $product->id;
$variation = new WC_Product_Variable( $product_id );
$arrvariations = $variation->get_children ();
// With foreach construct the div that will contain the Custom Fields
foreach ($arrvariations as $varid) {
$cfvalone = get_post_meta( $varid, '_custom_field_one', true );
$cfvaltwo = get_post_meta( $varid, '_custom_field_two', true );
// Check the Custom Fields are not empty
if (!empty($cfvalone) or !empty($cfvaltwo) ) {
$cfonecont = get_post_meta( $varid, '_custom_field_one', true );
$cftwocont = get_post_meta( $varid, '_custom_field_two', true );
$varsku = get_post_meta( $varid, '_sku', true );
// Built the DIV and embed SKU of the variation to be processed later by JS.
?>
<div class="varskudiv" data-varskudiv="<? echo $varsku;?>" style="display:none;">
<?php if (!empty($cfonecont)) {?>
CUSTOM FIELD ONE
<?php } ?>
<?php if (!empty($cftwocont)) {?>
CUSTOM FIELD TWO
<?php } ?>
</div>
<? }}?>
<br/>
<script>
jQuery(document).ready(function( $ ) {
// As we will take the contents of SPAN "sku" to create the JS
//we must consider that this SPAN is complete once the screen is loaded.
$(window).bind("load", function() {
woosku = $(".sku").text();
// Now we map the DIV we created "varskudiv" and load those values in an ARRAY
wooarrsku = $('div.varskudiv').map(function(){
return $(this).data('varskudiv');
}).get();
// Now we make loop, if the values match show the DIV.
var indexsku;
for (indexsku = 0; indexsku < wooarrsku.length; indexsku++) {
if (woosku == wooarrsku[indexsku]) {
$('.varskudiv[data-varskudiv="'+ woosku +'"]').css( "display", "inline-block" );
}
}
});
// Once loaded the screen, if the SPAN "sku" changes, start the process again and hide the previous DIV displayed.
$('.sku').bind("DOMSubtreeModified",function(){
woosku = $(".sku").text();
wooarrsku = $('div.varskudiv').map(function(){
return $(this).data('varskudiv');
}).get();
var indexsku;
for (indexsku = 0; indexsku < wooarrsku.length; indexsku++) {
if (woosku == wooarrsku[indexsku]) {
$('.varskudiv[data-varskudiv="'+ woosku +'"]').css( "display", "inline-block" );
}
else {$('.varskudiv[data-varskudiv="'+ wooarrsku[indexsku] +'"]').css( "display", "none" );
}
}
});
});
</script>

Get Permalink by Slug function not working

I hadn't realised that WordPress had added a native function for it to the Codex, but for some odd reason the page is blank. Does that mean the functionality is still coming or the page was added by mistake?
http://codex.wordpress.org/Function_Reference/get_permalink_by_slug
It's blank because it doesn't exist. If you change that function name to any random name you like you'll still see a blank page (unless of course the name you change it to is a real function).
function get_permalink_by_slug( $slug, $post_type = '' ) {
// Initialize the permalink value
$permalink = null;
// Build the arguments for WP_Query
$args = array(
'name' => $slug,
'max_num_posts' => 1
);
// If the optional argument is set, add it to the arguments array
if( '' != $post_type ) {
$args = array_merge( $args, array( 'post_type' => $post_type ) );
} // end if
// Run the query (and reset it)
$query = new WP_Query( $args );
if( $query->have_posts() ) {
$query->the_post();
$permalink = get_permalink( get_the_ID() );
} // end if
wp_reset_postdata();
return $permalink;
}

Wordpress custom metabox input value with AJAX

I am using Wordpress 3.5, I have a custom post (sp_product) with a metabox and some input field. One of those input (sp_title).
I want to Search by the custom post title name by typing in my input (sp_title) field and when i press add button (that also in my custom meta box), It will find that post by that Title name and bring some post meta data into this Meta box and show into other field.
Here in this picture (Example)
Search
Click Button
Get some value by AJAX from a custom post.
Please give me a example code (just simple)
I will search a simple custom post Title,
Click a button
Get the Title of that post (that i search or match) with any other post meta value, By AJAX (jQuery-AJAX).
Please Help me.
I was able to find the lead because one of my plugins uses something similar to Re-attach images.
So, the relevant Javascript function is findPosts.open('action','find_posts').
It doesn't seem well documented, and I could only found two articles about it:
Find Posts Dialog Box
Using Built-in Post Finder in Plugins
Tried to implement both code samples, the modal window opens but dumps a -1 error. And that's because the Ajax call is not passing the check_ajax_referer in the function wp_ajax_find_posts.
So, the following works and it's based on the second article. But it has a security breach that has to be tackled, which is wp_nonce_field --> check_ajax_referer. It is indicated in the code comments.
To open the Post Selector, double click the text field.
The jQuery Select needs to be worked out.
Plugin file
add_action( 'load-post.php', 'enqueue_scripts_so_14416409' );
add_action( 'add_meta_boxes', 'add_custom_box_so_14416409' );
add_action( 'wp_ajax_find_posts', 'replace_default_ajax_so_14416409', 1 );
/* Scripts */
function enqueue_scripts_so_14416409() {
# Enqueue scripts
wp_enqueue_script( 'open-posts-scripts', plugins_url('open-posts.js', __FILE__), array('media', 'wp-ajax-response'), '0.1', true );
# Add the finder dialog box
add_action( 'admin_footer', 'find_posts_div', 99 );
}
/* Meta box create */
function add_custom_box_so_14416409()
{
add_meta_box(
'sectionid_so_14416409',
__( 'Select a Post' ),
'inner_custom_box_so_14416409',
'post'
);
}
/* Meta box content */
function inner_custom_box_so_14416409( $post )
{
?>
<form id="emc2pdc_form" method="post" action="">
<?php wp_nonce_field( 'find-posts', '_ajax_nonce', false); ?>
<input type="text" name="kc-find-post" id="kc-find-post" class="kc-find-post">
</form>
<?php
}
/* Ajax replacement - Verbatim copy from wp_ajax_find_posts() */
function replace_default_ajax_so_14416409()
{
global $wpdb;
// SECURITY BREACH
// check_ajax_referer( '_ajax_nonce' );
$post_types = get_post_types( array( 'public' => true ), 'objects' );
unset( $post_types['attachment'] );
$s = stripslashes( $_POST['ps'] );
$searchand = $search = '';
$args = array(
'post_type' => array_keys( $post_types ),
'post_status' => 'any',
'posts_per_page' => 50,
);
if ( '' !== $s )
$args['s'] = $s;
$posts = get_posts( $args );
if ( ! $posts )
wp_die( __('No items found.') );
$html = '<table class="widefat" cellspacing="0"><thead><tr><th class="found-radio"><br /></th><th>'.__('Title').'</th><th class="no-break">'.__('Type').'</th><th class="no-break">'.__('Date').'</th><th class="no-break">'.__('Status').'</th></tr></thead><tbody>';
foreach ( $posts as $post ) {
$title = trim( $post->post_title ) ? $post->post_title : __( '(no title)' );
switch ( $post->post_status ) {
case 'publish' :
case 'private' :
$stat = __('Published');
break;
case 'future' :
$stat = __('Scheduled');
break;
case 'pending' :
$stat = __('Pending Review');
break;
case 'draft' :
$stat = __('Draft');
break;
}
if ( '0000-00-00 00:00:00' == $post->post_date ) {
$time = '';
} else {
/* translators: date format in table columns, see http://php.net/date */
$time = mysql2date(__('Y/m/d'), $post->post_date);
}
$html .= '<tr class="found-posts"><td class="found-radio"><input type="radio" id="found-'.$post->ID.'" name="found_post_id" value="' . esc_attr($post->ID) . '"></td>';
$html .= '<td><label for="found-'.$post->ID.'">' . esc_html( $title ) . '</label></td><td class="no-break">' . esc_html( $post_types[$post->post_type]->labels->singular_name ) . '</td><td class="no-break">'.esc_html( $time ) . '</td><td class="no-break">' . esc_html( $stat ). ' </td></tr>' . "\n\n";
}
$html .= '</tbody></table>';
$x = new WP_Ajax_Response();
$x->add( array(
'data' => $html
));
$x->send();
}
Javascript file open-posts.js
jQuery(document).ready(function($) {
// Find posts
var $findBox = $('#find-posts'),
$found = $('#find-posts-response'),
$findBoxSubmit = $('#find-posts-submit');
// Open
$('input.kc-find-post').live('dblclick', function() {
$findBox.data('kcTarget', $(this));
findPosts.open();
});
// Insert
$findBoxSubmit.click(function(e) {
e.preventDefault();
// Be nice!
if ( !$findBox.data('kcTarget') )
return;
var $selected = $found.find('input:checked');
if ( !$selected.length )
return false;
var $target = $findBox.data('kcTarget'),
current = $target.val(),
current = current === '' ? [] : current.split(','),
newID = $selected.val();
if ( $.inArray(newID, current) < 0 ) {
current.push(newID);
$target.val( current.join(',') );
}
});
// Double click on the radios
$('input[name="found_post_id"]', $findBox).live('dblclick', function() {
$findBoxSubmit.trigger('click');
});
// Close
$( '#find-posts-close' ).click(function() {
$findBox.removeData('kcTarget');
});
});

Resources