• Skip to main navigation
  • Skip to main content
  • Jump to the main sidebar

Carlosmdh

  • Migra ahora a Google Analytics 4
  • Blog
  • Snippets
  • Tutorials
  • Q&A GA4
  • About Me
  • Contact Me

Introduction to WordPress snippets

7 junio, 2021

When we talk about snippet we are talking about code fragments that we can insert in our WordPress to modify or add some function to our website. The most common when we talk about snippets is to associate them to PHP code snippets, but we can also associate them to other languages, such as CSS or JavaScript.

Usually they are only a few lines long, but we can extend them as much as we want. We could say that a plugin is a concatenation of snippets.

Focusing on PHP snippets, we can add them in three main ways:

  • Adding it to the functions.php file of our theme. It is best to do this at the end of the theme.
  • Mediante un plugin, como por ejemplo: Code Snippets,
  • Creando un plugin de funciones personalizadas, como ya comenté en otro artículo.
Code Snippets

Personally, whenever possible, I prefer the latter option and, as mentioned in the article I mention in the third option, better in a mu-plugin.

From there, the possibilities are endless, anything you can think of that you want to do, from preloading external DNS to adding Google Tag Manager scripts, Google Analytics .... or even adding an additional field to your product page in WooCommerce. Here, your imagination is the limit. Here are a couple of examples to give you an idea.

<?php
// Eliminar el numero de la versión de WordPress
remove_action('wp_head', 'wp_generator');
?>
<?php
/*
Plugin Name: Insertar Global Analytics en WordPress
Plugin URI: https://carlosmdh.es
Description: Plugin para insertar los Scripts de Global Analytics
Version: 1.0.1
Author: carlosmdh
License: GPL 2+
License URI:https://carlosmdh.es
*/
// Añadir el código de Global Analytics en el <head>
add_action( 'wp_head', 'carlosmdh_global_analytics' );
function carlosmdh_global_analytics() { ?>
<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=INSERTA AQUI EL IDENTIFICADOR DE TU PROPIEDAD DE ANALYTICS"></script>
<script>
  window.dataLayer = window.dataLayer || [];
  function gtag(){dataLayer.push(arguments);}
  gtag('js', new Date());
  
  gtag('config', 'INSERTA AQUI EL IDENTIFICADOR DE TU PROPIEDAD DE UNIVERSAL ANALYTICS UA-XXXXXXXXX-Y', { 'anonymize_ip': true }); // Identificador de Google Analytics 3 (Universal Analytics
  gtag('config', 'INSERTA AQUI EL IDENTIFICADOR DE TU PROPIEDAD DE GA4 G-XXXXXXXXXX'); //Identificador de Google Analytics 4 (GA4)
</script>
<!--End Global site tag (gtag.js) - Google Analytics -->
<?php }
?>

Don't go yet

I invite you to leave your impressions and/or doubts in the contact form and to suggest new topics that you would like me to cover in these tutorials. I will be happy to answer you by email and write in this blog.

No category

Sidebar

Sidebar-Alt

Blog - Snippets - Tutorials

Privacy Policy - Cookie Policy - Change your Consent


carlosmdh © 2023 · Made with by carlosmdh

We use cookies to give you the best experience on our website.

You can learn more about which cookies we use or disable them in the .

Carlosmdh
Powered by  GDPR Cookie Compliance
Privacy summary

This website uses cookies so that we can provide you with the best possible user experience. Cookie information is stored in your browser and performs functions such as recognising you when you return to our site or helping our team understand which parts of the site you find most interesting and useful.

Strictly necessary cookies

Strictly necessary cookies must always be enabled so that we can save your cookie settings preferences.

If you disable this cookie we will not be able to save your preferences. This means that each time you visit this website you will have to enable or disable cookies again.

Cookies de terceros

Esta web utiliza Google Analytics para recopilar información anónima tal como el número de visitantes del sitio, o las páginas más populares.

Dejar esta cookie activa nos permite mejorar nuestra web.

¡Por favor, activa primero las cookies estrictamente necesarias para que podamos guardar tus preferencias!

Cookies adicionales

Esta web utiliza las siguientes cookies adicionales:

(Lista aquí las cookies que estás utilizando en la web.)

¡Por favor, activa primero las cookies estrictamente necesarias para que podamos guardar tus preferencias!

  • Español
  • Français