Skip to content

Instantly share code, notes, and snippets.

@xtofr
Last active January 4, 2017 15:38
Show Gist options
  • Save xtofr/928824f1a521f4285221db2947352919 to your computer and use it in GitHub Desktop.
Save xtofr/928824f1a521f4285221db2947352919 to your computer and use it in GitHub Desktop.
Question on searchWP
<?php
/* ****************************** Généralités Admin WP ****************************** */
/* ************** Personnalisation du formulaire de cxion WP */
function my_login_stylesheet() {
wp_enqueue_style( 'custom-login', 'http://placedesqhse.net/wp-content/themes/maorigraphe/maori-login.css' );
//wp_enqueue_script( 'custom-login', get_template_directory_uri() . '/maori-login.js' );
}
add_action( 'login_enqueue_scripts', 'my_login_stylesheet' );
function my_login_logo_url() {
return home_url();
}
add_filter( 'login_headerurl', 'my_login_logo_url' );
function my_login_logo_url_title() {
return 'Place des QHSE';
}
add_filter( 'login_headertitle', 'my_login_logo_url_title' );
/* ************** changement expediteur Wordpress dans mails from WP */
add_filter('wp_mail_from', 'new_mail_from');
add_filter('wp_mail_from_name', 'new_mail_from_name');
function new_mail_from($old) {
return 'wordpress@placedesqhse.net';
}
function new_mail_from_name($old) {
return 'administrateur Place Des QHSE';
}
/* ***************************** SHORTCODES personnalisés ************** $user=wp_get_current_user(); */
add_shortcode( 'xtof' , 'ss_get_current_username' );
function ss_get_current_username(){
$user = wp_get_current_user();
$raison= xprofile_get_field_data( 'Raison sociale' , bp_get_member_user_id() );//fetch the text for Raison sociale
return $user->user_firstname . ' ' . $user->user_lastname . ' ';
}
/* ****************************** fin Généralités Admin WP ****************************** */
/* ***************************** SearchWP *********************************************** */
// highlight term extrait - excerpt
function searchwp_term_highlight_auto_excerpt( $excerpt ) {
global $post;
if ( ! is_search() ) {
return $excerpt;
}
// prevent recursion
remove_filter( 'get_the_excerpt', 'searchwp_term_highlight_auto_excerpt' );
$global_excerpt = searchwp_term_highlight_get_the_excerpt_global( $post->ID, null, get_search_query() );
add_filter( 'get_the_excerpt', 'searchwp_term_highlight_auto_excerpt' );
return $global_excerpt;
}
add_filter( 'get_the_excerpt', 'searchwp_term_highlight_auto_excerpt' );
// augmentation du nombre de mots dans l'extrait
function my_searchwp_th_num_words() {
// use x words instead of the default 55
return 300;
}
// retourne un extrait du pdf
function maybe_append_searchwp_pdf_excerpt( $excerpt ) {
global $post;
$pdf_excerpt_length = 15; // number of words in PDF excerpt
if ( is_search() && ! post_password_required() ) {
// prep our 'environment'
// set up common words
$common_words = array();
if ( class_exists( 'SearchWP' ) ) {
$searchwp = SearchWP::instance();
$common_words = $searchwp->common;
}
// grab the terms
$terms = explode( ' ', get_search_query() );
$terms = array_map( 'sanitize_text_field', $terms );
// if we're on a search page, we want to check to see if the current result
// has a PDF with any of the search terms in the content
// first we need to backtrack and find all of the PDFs that are attached to this post
// since their weight has been attributed to this post
$attached_pdfs = get_attached_media( 'application/pdf', $post->ID );
foreach ( $attached_pdfs as $attached_pdf ) {
// check to make sure there is file content to scan
if ( $pdf_content = get_post_meta( $attached_pdf->ID, 'searchwp_content', true ) ) {
// find the first applicable search term (based on character length)
$flag = false;
foreach ( $terms as $termkey => $term ) {
if ( ! in_array( $term, $common_words ) && absint( apply_filters( 'searchwp_minimum_word_length', 3 ) ) <= strlen( $term ) ) {
$flag = $term;
break;
}
}
// our haystack is the PDF content
$haystack = explode( ' ', $pdf_content );
$pdf_excerpt = '';
// build our contextual excerpt
foreach ( $haystack as $haystack_key => $haystack_term ) {
preg_match( "/\b$flag\b(?!([^<]+)?>)/i", $haystack_term, $matches );
if ( count( $matches ) ) {
// our buffer is going to be 1/3 the total number of words in hopes of snagging one or two more
// highlighted terms in the second and third thirds
$buffer = floor( ( $pdf_excerpt_length - 1 ) / 3 ); // -1 to accommodate the search term itself
// find the start point
$start = 0;
$underflow = 0;
if ( $haystack_key < $buffer ) {
// the match occurred too early to get a proper first buffer
$underflow = $buffer - $haystack_key;
} else {
// there is enough room to grab a proper first buffer
$start = $haystack_key - $buffer;
}
// find the end point
$end = count( $haystack );
if ( $end > ( $haystack_key + ( $buffer * 2 ) ) ) {
$end = $haystack_key + ( $buffer * 2 );
}
// if we had an underflow (e.g. the first buffer wasn't fully included) grab more at the end
$end += $underflow;
$pdf_excerpt = array_slice( $haystack, $start, $end - $start );
$pdf_excerpt = implode( ' ', $pdf_excerpt );
break;
}
}
// append our PDF-specific excerpt to the main excerpt
if ( ! empty( $pdf_excerpt ) ) {
$pdf_label = get_the_title( $attached_pdf->ID ); // the PDF label will be the title of the PDF post
$excerpt .= '<br /><br /><strong>' . $pdf_label . '</strong>: ' . $pdf_excerpt;
}
}
}
}
return $excerpt;
}
add_filter( 'get_the_excerpt', 'maybe_append_searchwp_pdf_excerpt' );
add_filter( 'searchwp_th_num_words', 'my_searchwp_th_num_words' );
// Automatically convert permalinks to PDFs in search results to the PDF itself, not the Attachment page
function my_force_direct_pdf_links( $permalink ){
global $post;
if ( is_search() && 'application/pdf' == get_post_mime_type( $post->ID ) ) {
// if the result is a PDF, link directly to the file not the attachment page
$permalink = wp_get_attachment_url( $post->ID );
}
return esc_url( $permalink );
}
add_filter( 'the_permalink', 'my_force_direct_pdf_links' );
add_filter( 'attachment_link', 'my_force_direct_pdf_links' );
/* ****************************** BB PRESS ************************************ */
/* activer editeur de texte from https://codex.bbpress.org/enable-visual-editor/ */
function bbp_enable_visual_editor( $args = array() ) {
$args['tinymce'] = true;
$args['teeny'] = false;
$args['quicktags'] = false;
return $args;
}
add_filter( 'bbp_after_get_the_content_parse_args', 'bbp_enable_visual_editor' );
/* filter to add description after forums titles on forum index https://codex.bbpress.org/layout-and-functionality-examples-you-can-use/ */
function rw_singleforum_description() {
echo '<div class="bbp-forum-content">';
echo bbp_forum_content();
echo '</div>';
}
add_action( 'bbp_template_before_single_forum' , 'rw_singleforum_description');
/* display bbPress search form above single topics and forums */
function rk_bbp_search_form(){
if ( bbp_allow_search()) {
?>
<div class="bbp-search-form">
<?php bbp_get_template_part( 'form', 'search' ); ?>
</div>
<?php
}
}
add_action( 'bbp_template_before_single_forum', 'rk_bbp_search_form' );
add_action( 'bbp_template_before_single_topic', 'rk_bbp_search_form' );
/* Icones pour les statuts des posts
Show status labels for bbPress Topics <img src="http://placedesqhse.net/wp-content/themes/jupiter/assets/icons/icomoon/svg/69.svg" alt="sticky" width="16px">
// mk-moon-stack-empty /mk-moon-stack-plus/ mk-icon-exclamation / mk-li-apeaker
*/
function rk_sticky_topics() {
if ( bbp_is_topic_sticky() && !bbp_is_topic_closed() )
echo '<span class="sticky"><img src="http://placedesqhse.net/wp-content/themes/jupiter/assets/icons/icomoon/svg/69.svg" alt="sticky" width="16px"></span>';
}
add_action( 'bbp_theme_before_topic_title', 'rk_sticky_topics' );
function rk_announcement_topics() {
if ( bbp_is_topic_sticky() && bbp_is_topic_closed() )
echo '<span class="announcement"><img src="http://placedesqhse.net/wp-content/themes/jupiter/assets/icons/pe-line-icons/svg/e662.svg" alt="announcement" width="16px"></span>';
}
add_action( 'bbp_theme_before_topic_title', 'rk_announcement_topics' );
function rk_new_topics() {
$offset = 60*60*12;
if ( get_post_time() > date('U') - $offset )
echo '<span class="new"><img src="http://placedesqhse.net/wp-content/themes/jupiter/assets/icons/icomoon/svg/e23b.svg" alt="new" width="16px"></span>';
}
add_action( 'bbp_theme_before_topic_title', 'rk_new_topics' );
/* ****************************** fin BB PRESS ************************************ */
/* **************************** Début Hooks Woocommerce *******************************/
/* https://docs.woocommerce.com/document/tutorial-customising-checkout-fields-using-actions-and-filters/ */
/* ***************************** Fin Hooks Woocommerce *******************************/
?>
<?php
/*
** search.php
** mk_build_main_wrapper : builds the main divisions that contains the content. Located in framework/helpers/global.php
** mk_get_view gets the parts of the pages, modules and components. Function located in framework/helpers/global.php
*/
get_header();
mk_build_main_wrapper( mk_get_view('templates', 'wp-search', true) );
get_footer();
<section class="mk-search-loop">
<section class="widget widget_search"><p><?php esc_html_e( 'Not so happy with results? Search for a new keyword ', 'mk_framework' ); ?></p>
<form class="mk-searchform" method="get" id="searchform" action="<?php echo home_url('/'); ?>">
<input type="text" class="text-input" placeholder="<?php esc_attr_e( 'Search site', 'mk_framework' ); ?>" value="" name="s" id="s" />
<i class="mk-searchform-icon"><?php Mk_SVG_Icons::get_svg_icon_by_class_name(true,'mk-icon-search',16); ?><input value="" class="search-button" type="submit" /></i>
</form>
</section>
<?php
if ( have_posts() ):
while ( have_posts() ) :
the_post();
$post_type = get_post_type();
?>
<article class="search-result-item">
<h4 class="the-title"><a href="<?php echo esc_url( get_permalink() ); ?>"><?php the_title(); ?></a></h4>
<div class="search-loop-meta">
<span><?php esc_html_e( 'By', 'mk_framework' ); ?> <?php the_author_posts_link(); ?></span>
<time datetime="<?php the_time('Y-m-d'); ?>">
<?php the_date('', '<time datetime="'.get_the_time().'">' . esc_html__( 'On', 'mk_framework' ) . ' <a href="'.get_month_link( get_the_time( "Y" ), get_the_time( "m" ) ).'">', '</a></time>'); ?>
</a>
</time>
<?php
echo '<span class="mk-search-cats">';
switch ($post_type) {
case 'post':
echo esc_html__( 'In', 'mk_framework' ) . ' '.get_the_category_list( ', ' );
break;
case 'portfolio':
echo esc_html__( 'In', 'mk_framework' ) . ' '.implode(', ', mk_get_custom_tax(get_the_id(), 'portfolio', true));
break;
case 'news':
echo esc_html__( 'In', 'mk_framework' ) . ' '.implode(', ', mk_get_custom_tax(get_the_id(), 'news', true));
break;
}
echo '</span>';
?>
</div>
<div class="the-excerpt"><p><?php mk_excerpt_max_charlength(300) ?></p></div>
</article>
<?php
$post_type = '';
endwhile;
mk_post_pagination(NULL);
wp_reset_query();
endif;
?>
</section>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment