Category Archives: functions.php

Add animated scroll-to-ID functionality

// add scroll to ID functionality
function load_script_to_scroll_to_id(){
?>

Woocommerce use same base url for products and categories

// use same base url for products and categories
function wpse_291143_generate_taxonomy_rewrite_rules( $wp_rewrite ) {

global $wp_rewrite;
$base = "shop";

$rules = array();
$terms = get_terms( array( 'taxonomy' => 'product_cat', 'hide_empty' => false ));

foreach($terms as $term) {
$term_children = get_terms( array(
'taxonomy' => 'product_cat',
'parent' => intval($term->term_id),
'hide_empty' => false
)
);
if($term_children) {
foreach ( $term_children as $term_child ) {
$rules[$base . '/' . $term->slug . '/' . $term_child->slug . '/?$'] = 'index.php?product_cat=' . $term_child->slug;
}
}
$rules[$base . '/' . $term->slug . '/?$'] = 'index.php?product_cat=' . $term->slug;
}

$wp_rewrite->rules = $rules + $wp_rewrite->rules;
return $wp_rewrite->rules;
}
add_action('generate_rewrite_rules', 'wpse_291143_generate_taxonomy_rewrite_rules');

Add “Products” to breadcrumb on woocommerce category pages

// add "Products" to breadcrumb on category pages and other missing levels
add_filter( 'wpseo_breadcrumb_links', 'wpseo_breadcrumb_add_woo_shop_link' );
function wpseo_breadcrumb_add_woo_shop_link( $links ) {
global $post;
if ( is_woocommerce()
&& !is_shop() // not top level shop page
&& !is_product() // not on single product page
) {
$breadcrumb[] = array(
'url' => get_permalink( woocommerce_get_page_id( 'shop' ) ),
'text' => 'Products',
);
array_splice( $links, 1, -2, $breadcrumb );
} elseif ( is_post_type_archive( 'news' ) || is_singular( 'news' ) ) {
$breadcrumb[] = array(
'url' => get_permalink( '29' ),
'text' => 'Information',
);
$breadcrumb[] = array(
'url' => get_permalink( '50' ),
'text' => 'Media',
);
array_splice( $links, 1, -2, $breadcrumb );
} elseif ( is_home() ) { // any other archive...
$breadcrumb[] = array(
'url' => get_permalink( '29' ),
'text' => 'Information',
);
$breadcrumb[] = array(
'url' => get_permalink( '50' ),
'text' => 'Media',
);
array_splice( $links, 1, -2, $breadcrumb );

} elseif ( is_singular( 'post' ) ) {
$breadcrumb[] = array(
'url' => get_permalink( '29' ),
'text' => 'Information',
);
$breadcrumb[] = array(
'url' => get_permalink( '50' ),
'text' => 'Media',
);
$breadcrumb[] = array(
'url' => get_permalink( '54' ),
'text' => 'Blog',
);
array_splice( $links, 1, -2, $breadcrumb );
}
return $links;
}

Link post types

// related posts
// see https://github.com/scribu/wp-posts-to-posts/wiki/Basic-usage
function my_connection_types() {
p2p_register_connection_type( array(
'name' => 'posts_to_pages',
'from' => 'clients',
'to' => 'portfolio'
) );
}
add_action( 'p2p_init', 'my_connection_types' );

Add custom posts to archive pages

// add custom posts to archive pages
function kweb_add_custom_types( $query ) {
if( is_category() || is_tag() && empty( $query->query_vars[‘suppress_filters’] ) ) {
$query->set( ‘post_type’, array(
‘post’, ‘clients’, ‘portfolio’
));
return $query;
}
}
add_filter( ‘pre_get_posts’, ‘kweb_add_custom_types’ );

Contact Form 7 add uploaded file to media & attach to custom post

Here:

function my_wpcf7_save($cfdata) {

$formtitle = $cfdata->title;

global $wpdb;
$form_to_DB = WPCF7_Submission::get_instance();
if ( $form_to_DB ) {
$formData = $form_to_DB->get_posted_data();
$uploaded_files = $form_to_DB->uploaded_files(); // this allows you access to the upload file in the temp location
}

if ( $formtitle == 'sync_test') {

// create a new post
$newpost = array(
'post_type' => 'students',
'post_title' => $formData['your-firstname']. ' ' .$formData['your-lastname']. ' - ' .$formData['your-company'],
'post_status' => 'pending');

$newpostid = wp_insert_post($newpost);

// add meta data for the new post
add_post_meta($newpostid, 'rli_meta_students_first_name', $formData['your-firstname']);
add_post_meta($newpostid, 'rli_meta_students_surname', $formData['your-lastname']);
add_post_meta($newpostid, 'rli_meta_students_email', $formData['your-email']);
...
add_post_meta($newpostid, 'rli_meta_students_notes', $formData['your-notes']);
//add_post_meta($newpostid, 'rli_meta_students_photo', $formData['ProfilePhoto']); -- do this after saving image in uploads

$image_name = $formData['ProfilePhoto'];
$image_location = $uploaded_files["ProfilePhoto"];
$image_content = file_get_contents($image_location);
if ($image_content==false){
$check = 'content fail';
} else {
$check = 'content success';
}
$wud = wp_upload_dir();
$upload = wp_upload_bits( $image_name, null, $image_content);
$chemin_final = $upload['url']; // http://www.xxxx.com/wp-content/uploads/2015/09/xxxx.jpg
$filename= $upload['file']; // /home/sites/xxxx.com/public_html/wp-content/uploads/wpcf7_uploads/1565907367/xxxx.jpg
if ($filename>'') {
require_once(ABSPATH . 'wp-admin/includes/admin.php');
$wp_filetype = wp_check_filetype(basename($filename), null );
$attachment = array(
'post_mime_type' => $wp_filetype['type'],
'post_title' => preg_replace('/\.[^.]+$/', '', basename($filename)),
'post_content' => '',
'post_status' => 'inherit'
);
$attach_id = wp_insert_attachment( $attachment, $filename, $newpostid);
require_once(ABSPATH . 'wp-admin/includes/image.php');
$attach_data = wp_generate_attachment_metadata( $attach_id, $filename );
wp_update_attachment_metadata( $attach_id, $attach_data );
update_post_meta($newpostid, "_thumbnail_id", $attach_id);
add_post_meta($newpostid, 'rli_meta_students_photo', $chemin_final); // $formData['ProfilePhoto']
}
}
}
add_action('wpcf7_before_send_mail', 'my_wpcf7_save',1);

added to functions.php

Add simple html to widget titles

Original code from

function html_widget_title( $title ) {
    //HTML tag opening/closing brackets
    $title = str_replace( '[', '<', $title );
    $title = str_replace( '[/', '</', $title );

    //<strong></strong>
    $title = str_replace( 's]', 'strong>', $title );
    //<em></em>
    $title = str_replace( 'e]', 'em>', $title );

    return $title;
}
add_filter( 'widget_title', 'html_widget_title' );

Amended code for line breaks only

function html_widget_title( $title ) {
    //HTML tag opening/closing brackets
    $title = str_replace( '[', '<', $title );
    $title = str_replace( 'br]', 'br>', $title );
    return $title;
}
add_filter( 'widget_title', 'html_widget_title' );