Colección de 10 Snippets básicos para casi toda web

53

Puede poner esos códigos en el archivo Function.php de su tema

Lista de recopilación de fragmentos de código de WordPress para el desarrollo de complementos y temas de WP

1. Eliminar el campo de URL de los comentarios

Evita que utilicen tu web para crear backlinks de SPAM, esto ayudará a que tengas comentarios reales y no generador por bots.

function remove_comment_fields($fields) {
unset($fields['url']); //Elimina la URL del comentario
return $fields;
}
add_filter('comment_form_default_fields','remove_comment_fields'); 
// Se implementa usando la función de filtro

2. Cambiar el LOGOTIPO en la página de login

Normalmente hacemos esto con un plugin pero de esta manera podemos evitar usa plugins.

function custom_login_logo() {
echo '<style type="text/css"> 
h1 a { background: url('.get_bloginfo('template_directory') . ' your logo image url here) 
50% 50% no-repeat !important; }
</style>';
}
add_action('login_head', 'custom_login_logo');

3. Cambiar el slug de autor

add_action('init', 'cng_author_base');
function cng_author_base() {
global $wp_rewrite;
$author_slug = 'profile'; // change slug name
$wp_rewrite->author_base = $author_slug;
}

4. Añade el código de AdSense a tu Blog

Si estas monetizando tu blog con anuncios de Google este snippet te será muy útil para implementar los anuncios y empezar a monetizar.

add_action( 'wp_head', function () { ?>
<script data-ad-client="AQUI_TU_ID_DE_ADSENSE" 
async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>

5. Vacía la papelera de WordPress usando código

define( 'EMPTY_TRASH_DAYS', 10 ); // 10 days

6. Inicia sesión con nombre de usuario o dirección de correo electrónico

function login_with_email_address($username) {
        $user = get_user_by('email',$username);
        if(!empty($user->user_login))
                $username = $user->user_login;
        return $username;
}
add_action('wp_authenticate','login_with_email_address');
function change_username_wps_text($text){
       if(in_array($GLOBALS['pagenow'], array('wp-login.php'))){
         if ($text == 'Username'){$text = 'Username / Email';}
            }
                return $text;
         }
add_filter( 'gettext', 'change_username_wps_text' );

7. Registra un Short Code para crear una lista desplegable de categorías de productos de Woocommerce

<?php

/**
 * WooCommerce Extra Feature
 * --------------------------
 *
 * Register a shortcode that creates a product categories dropdown list
 *
 * Use: [product_categories_dropdown orderby="title" count="0" hierarchical="0"]
 *
 */
add_shortcode( 'product_categories_dropdown', 'woo_product_categories_dropdown' );

function woo_product_categories_dropdown( $atts ) {

  extract(shortcode_atts(array(
    'count'         => '0',
    'hierarchical'  => '0',
    'orderby' 	    => ''
    ), $atts));
	
	ob_start();
	
	$c = $count;
	$h = $hierarchical;
	$o = ( isset( $orderby ) && $orderby != '' ) ? $orderby : 'order';
		
	// Stuck with this until a fix for https://core.trac.wordpress.org/ticket/13258
	woocommerce_product_dropdown_categories( $c, $h, 0, $o );

	?>
	<script type='text/javascript'>
	/* <![CDATA[ */
	var product_cat_dropdown = document.getElementById("dropdown_product_cat");
	function onProductCatChange() {
		if ( product_cat_dropdown.options[product_cat_dropdown.selectedIndex].value !=='' ) {
		location.href = "<?php echo home_url(); ?>
		/?product_cat="+product_cat_dropdown.options[product_cat_dropdown.selectedIndex].value;
		}
		}
		product_cat_dropdown.onchange = onProductCatChange;
	/* ]]> */
	</script>
	<?php
	
	return ob_get_clean();
	
}

8. Códigos de eco de información de correo electrónico de Woocommerce para plantillas de correo electrónico

//Code For Order ID
<?php echo $order->id; ?>
 
//Code For Order Date
<?php echo $order->order_date; ?>
 
//Code For Shipping First and Last Name
<?php echo $order->shipping_first_name . " " . $order->shipping_last_name; ?>
 
//Code For Shipping address
<?php echo $order->shipping_address_1; ?>
 
//Code For Shipping Apartment Number
<?php if($order->shipping_address_2 != ""){ echo '<br>' . $order->shipping_address_2;}?>
 
//Code For Shipping Country
<?php $countries = new WC_Countries; $shipping_country = $order->shipping_country;
echo ( $shipping_country && isset( $countries->countries[ $shipping_country ] ) ) ? 
$countries->countries[ $shipping_country ] : $shipping_country; ?>
 
//Code For Billing First and Last Name
<?php echo $order->billing_first_name . " " . $order->billing_last_name; ?>
 
//Code For Billing address
<?php echo $order->billing_address_1; ?>
 
//Code For Billing Apartment Number
<?php if($order->billing_address_2 != ""){ echo '<br>' . $order->billing_address_2;}?>
 
//Code For Billing Country
<?php $countries = new WC_Countries; $billing_country = $order->billing_country; 
echo ( $billing_country && isset( $countries->countries[ $billing_country ] ) ) ? 
$countries->countries[ $billing_country ] : $billing_country; ?>
 
//Code For Order Items/Products
<?php do_action( 'woocommerce_email_order_details', $order, $sent_to_admin, $plain_text, $email ); ?>

9. Agrega un símbolo de moneda personalizado en la tienda Woocommerce

add_filter('woocommerce_currency_symbol', 'change_existing_currency_symbol', 10, 2);
function change_existing_currency_symbol( $currency_symbol, $currency ) {
  switch( $currency ) {
    case 'AUD': $currency_symbol = 'AUD$'; break;
  }
  return $currency_symbol;
}

10. Crea una página personalizada después de la activación del tema

if (isset($_GET['activated']) && is_admin()){
        $new_page_title = 'This is the page title';
        $new_page_content = 'This is the page content';
        $new_page_template = ''; //ex. template-custom.php. Leave blank if you don't want a custom page template.
        //don't change the code bellow, unless you know what you're doing
        $page_check = get_page_by_title($new_page_title);
        $new_page = array(
                'post_type' => 'page',
                'post_title' => $new_page_title,
                'post_content' => $new_page_content,
                'post_status' => 'publish',
                'post_author' => 1,
        );
        if(!isset($page_check->ID)){
                $new_page_id = wp_insert_post($new_page);
                if(!empty($new_page_template)){
                        update_post_meta($new_page_id, '_wp_page_template', $new_page_template);
                }
        }
}
close
I Love WordPress isotipo 256

Regístrate para recibir actualizaciones en tu correo.

¡No enviamos spam! Lee nuestra política de privacidad para más información.

Cargando...

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More