HEX
Server: Apache/2.4.65 (Debian)
System: Linux 88f31f35b0b8 6.1.0-38-amd64 #1 SMP PREEMPT_DYNAMIC Debian 6.1.147-1 (2025-08-02) x86_64
User: www-data (33)
PHP: 8.2.29
Disabled: NONE
Upload Files
File: /var/www/html/wp-content/themes/Newspaper-child/functions.php
<?php
/*  ----------------------------------------------------------------------------
    Newspaper V9.0+ Child theme - Please do not use this child theme with older versions of Newspaper Theme

    What can be overwritten via the child theme:
     - everything from /parts folder
     - all the loops (loop.php loop-single-1.php) etc
	 - please read the child theme documentation: http://forum.tagdiv.com/the-child-theme-support-tutorial/

 */


//function fb_disable_feed() {
//     wp_die( __('No feed available, please visit our <a href="'. get_bloginfo('url') .'">homepage</a>!') );
//}

//add_action('do_feed', 'fb_disable_feed', 1);
//add_action('do_feed_rdf', 'fb_disable_feed', 1);
//add_action('do_feed_rss', 'fb_disable_feed', 1);
//add_action('do_feed_rss2', 'fb_disable_feed', 1);
//add_action('do_feed_atom', 'fb_disable_feed', 1);

/*  ----------------------------------------------------------------------------
    add the parent style + style.css from this folder
 */

function general_admin_notice(){
    global $pagenow;
    if ( $pagenow == 'themes.php' ) {
         echo '<div class="notice notice-warning" style="background:#0000ff; color:#ffffff; padding-top: 20px;">
		     <img src="https://www.oimmei.com/wp-content/uploads/2023/12/oimmei-digital-consulting-new.png" style="height: 45px">
             <p>Procedura per aggiornare il tema</p>
			 <ol>
			 	<li>Concordare orario serale con la redazione</li>
				<li>Al momento dell\'aggiornamento non devono esserci articoli programmati</li>
				<li>All\'orario concordato fare partire il backup</li>
				<li>Terminato il backup aggiornare il tema, e a seguito fare gli aggiornamenti dei plugin del tema</li>
				<li>Ripristinato l\'aspetto grafico del tema, verificare i contenuti con il backup</li>
				<li>Avvisare la redazione che l\'aggiornamento è terminato</li>
			 </ol>
         </div>';
    }
}
add_action('admin_notices', 'general_admin_notice');

add_action( 'wp_enqueue_scripts', 'theme_enqueue_styles', 11);
function theme_enqueue_styles() {
    wp_enqueue_style('td-theme', get_template_directory_uri() . '/style.css', '', TD_THEME_VERSION, 'all' );
    wp_enqueue_style('td-theme-child', get_stylesheet_directory_uri() . '/style.css', array('td-theme'), TD_THEME_VERSION . 'c', 'all' );
}

function custom_taxonomy() {
    add_rewrite_tag('%place%', '([^&]+)', 'places=');
	
	// articolo
	add_rewrite_rule('^(?!wp-json/)([^/])/([^/])/([^/]*)/?','index.php?places=$matches[1]&category=$matches=[2]&name=$matches[3]', 'top');
	
	// articoli luogo/categoria
	add_rewrite_rule('^(?!wp-json/)((?!(feed|luogo|notizie|author))[^/]+)/([^/]+)/page/([0-9]{1,})/?', 'index.php?places=$matches[1]&category=$matches[2]&paged=$matches[3]', 'top' );
	
	// articoli luogo
	add_rewrite_rule('^(?!wp-json/)((?!(feed|luogo|notizie|author))[^/]+)/page/([0-9]{1,})/?', 'index.php?places=$matches[1]&paged=$matches[2]', 'top' );

	// articoli luogo (deprecato)
	add_rewrite_rule('^(?!wp-json/)place/([^/]+)/?', 'index.php?places=$matches[1]', 'top' );
	add_rewrite_rule('^(?!wp-json/)luogo/([^/]+)/?', 'index.php?places=$matches[1]', 'top' );

	// feed generico
	add_rewrite_rule('^(?!wp-json/)feed/?', 'index.php?feed=rss2', 'top' );

    /* $label contains parameters determining the Taxonomy’s display name
    */
    $labels = array(
        'name' => 'Luoghi',
        'singular' => 'Luogo',
        'menu_name' => 'Luoghi'
    );

    /* $args declares various parameters in the custom taxonomy
    */
    $args = array(
        'labels' => $labels,
        'hierarchical' => true,
		'public' => true,
        'show_ui' => true,
        'show_admin_column' => true,
        'show_in_nav_menus' => true,
        'show_tagcloud' => true,
		'query_var' => true,
		'rewrite' => array('slug' => 'luogo'),	
		'with_front' => true
    );

    /* register_taxonomy() to register taxonomy
    */
    register_taxonomy('places', 'post', $args);
	$GLOBALS['wp_rewrite']->use_verbose_page_rules = true; //fix
}

// // Hook into the 'init' action
add_action( 'init', 'custom_taxonomy', 0 ); 

// Assign value to %place% rewrite tag
add_filter('post_link', 'my_filter_post_link', 10, 2 );
function my_filter_post_link($permalink, $post) {
    $permalink = str_replace("%25", "%", urldecode($permalink));

    // Esce se non c'è il tag %place% nell'URL
    if (false === strpos($permalink, '%place%')) {
        return $permalink;
    }

    // Cerca prima la località principale impostata con Yoast
    $primary_term_id = get_post_meta($post->ID, '_yoast_wpseo_primary_places', true);
    
    if ($primary_term_id) {
        // Recupera il termine principale
        $primary_term = get_term($primary_term_id, 'places');
        if (!is_wp_error($primary_term) && $primary_term) {
            $location = $primary_term->slug;
        }
    }

    // Se non trova una località principale, usa il comportamento di fallback originale
    if (!isset($location)) {
        $terms = wp_get_post_terms($post->ID, 'places');
        if (count($terms) > 0) {
            $location = $terms[0]->slug;
        } else {
            $location = 'italia-mondo';
        }
    }

    $location = urlencode($location);
    $permalink = str_replace('%place%', $location, $permalink);
    return $permalink;
}

add_action('restrict_manage_posts', 'filter_post_by_place');
function filter_post_by_place() {
	global $typenow;
	$post_type = 'post';
	$taxonomy  = 'places';
	if ($typenow == $post_type) {
		$selected      = isset($_GET[$taxonomy]) ? $_GET[$taxonomy] : '';
		$info_taxonomy = get_taxonomy($taxonomy);
		wp_dropdown_categories(array(
			'show_option_all' => sprintf( __( 'Tutti i %s', 'textdomain' ), $info_taxonomy->label ),
			'show_option_none' => sprintf( __( 'No %s', 'textdomain' ), $info_taxonomy->label ),
			'option_none_value' => -1,
			'taxonomy'        => $taxonomy,
			'name'            => $taxonomy,
			'orderby'         => 'name',
			'selected'        => $selected,
			'show_count'      => true,
			'hide_empty'      => true,
		));
	};
}
/**
 * Filter posts by place in admin
 */
add_filter('parse_query', 'tsm_convert_id_to_term_in_query');
function tsm_convert_id_to_term_in_query($query) {
	global $pagenow;
	$post_type = 'post';
	$taxonomy  = 'places';
	$q_vars    = &$query->query_vars;
	if ( $pagenow == 'edit.php' && isset($q_vars['post_type']) && $q_vars['post_type'] == $post_type && isset($q_vars[$taxonomy]) && is_numeric($q_vars[$taxonomy]) && $q_vars[$taxonomy] != 0 ) {
		if ($q_vars[$taxonomy] == -1) {
			$all_terms = get_terms(['taxonomy'=>$taxonomy, 'fields'=>'ids']);
    		$tax_query = array(array(
        		'taxonomy' => $taxonomy,
        		'field' => 'term_id',
        		'terms' => $all_terms,
        		'operator' => 'NOT IN',
    	));
    	$query->set( 'tax_query', $tax_query );	
		}
		$term = get_term_by('id', $q_vars[$taxonomy], $taxonomy);
		$q_vars[$taxonomy] = $term->slug;
	}
}

/* text search posts only in title */
add_filter( 'posts_search', 'search_by_title_only', 10, 2 );
function search_by_title_only( $search, $wp_query ) {
    if ( !is_admin() || empty( $search ) ){
        return $search;
    }
    global $wpdb;
    $q = $wp_query->query_vars;
    $x = ! empty( $q['exact'] ) ? '' : '%';
    $search = '';
    $searchand = '';
    foreach ( (array) $q['search_terms'] as $term ) {
        $term = esc_sql( $wpdb->esc_like( $term ) );
        $search .= "{$searchand}($wpdb->posts.post_title LIKE '{$x}{$term}{$x}')";
        $searchand = ' AND ';
    }
    if ( ! empty( $search ) ) {
        $search = " AND ({$search}) ";
    }
    return $search;
}


/* add_filter('wp_terms_checklist_args', 'display_custom_checklist');
function display_custom_checklist( $args ){
    if ( $args['taxonomy'] == 'category' )
    $args['walker'] = new my_custom_walk;
    return $args;
}

class my_custom_walk extends Walker {
    var $tree_type = 'category';
    var $db_fields = array ('parent' => 'parent', 'id' => 'term_id'); //TODO: decouple this

    function start_lvl( &$output, $depth = 0, $args = array() ) {
        $indent = str_repeat("\t", $depth);
        $output .= "$indent<ul class='children'>\n";
    }

    function end_lvl( &$output, $depth = 0, $args = array() ) {
        $indent = str_repeat("\t", $depth);
        $output .= "$indent</ul>\n";
    }

    function start_el( &$output, $category, $depth, $args, $id = 0 ) {
        extract($args);
        if ( empty($taxonomy) )
            $taxonomy = 'category';

        if ( $taxonomy == 'category' )
            $name = 'post_category';
        else
            $name = 'tax_input['.$taxonomy.']';

        $class = in_array( $category->term_id, $popular_cats ) ? ' class="popular-category"' : '';
        $output .= "\n<li id='{$taxonomy}-{$category->term_id}'$class>" . '<label class="selectit"><input value="' . $category->term_id . '" type="checkbox" name="'.$name.'[]" id="in-'.$taxonomy.'-' . $category->term_id . '"' . checked( in_array( $category->term_id, $selected_cats ), true, false ) . disabled( empty( $args['disabled'] ), false, false ) . ' /> ' . esc_html( apply_filters('the_category', $category->name.' - '.$category->slug )) . '</label>';
    }

    function end_el( &$output, $category, $depth = 0, $args = array() ) {
        $output .= "</li>\n";
    }
} */