Skip to content

Instantly share code, notes, and snippets.

@caralgar
caralgar / get-slug.php
Created September 29, 2015 08:35
Get Slug for WordPress page
<?php
$post = get_post($post_id);
$slug = $post->post_name;
?>
@caralgar
caralgar / functions.php
Last active October 14, 2015 07:22
dequeue css, deregister style
<?php
function dequeue_css() {
wp_dequeue_style('child-style');
wp_deregister_style('child-style');
}
add_action('wp_enqueue_scripts','dequeue_css',1002);
?>
@caralgar
caralgar / function.php
Created October 14, 2015 07:23
Bold first word jquery in WordPress
<?php
function bold_first_word() {
$out = "
<script type=\"text/javascript\">
jQuery('#content h1').html(function(i,html){
return html.replace(/^\s*([^\s]+)(\s|$)/, '<span class=\"first-word\">$1 </span>');
});
</script>
";
echo $out;
@caralgar
caralgar / post.php
Created October 21, 2015 15:27
Cómo obtener el nombre de una taxonomía
<?php
$terms = get_the_terms( $post->ID , 'types' ); // 'types' es el nombre de la taxonomia
if($terms) {
foreach( $terms as $term ) {
echo $term->name.'<br />';
}
}
?>
@caralgar
caralgar / functions.php
Created November 4, 2015 14:50
How to remove Category:, tag:, etc from the_archive_title()
<?php
add_filter( 'get_the_archive_title', function ($title) {
if ( is_category() ) {
$title = single_cat_title( '', false );
} elseif ( is_tag() ) {
$title = single_tag_title( '', false );
} elseif ( is_author() ) {
$title = '<span class="vcard">' . get_the_author() . '</span>' ;
}
return $title;
@caralgar
caralgar / header.php
Created November 9, 2015 11:38
Get post meta for custom body id
<?php
$body_id = get_post_meta($post->ID,'body_id');
?>
<body <?php body_class() ?> <?php if (!empty($body_id[0])) echo 'id="'.$body_id[0].'"' ?>>
@caralgar
caralgar / functions.php
Created November 18, 2015 08:28
How to show title on images (general filter) - wp_get_attachment_image
<?php
add_filter( 'wp_get_attachment_image_attributes', 'add_title_to_attachment_image', 10, 2 );
function add_title_to_attachment_image( $attr, $attachment ) {
$attr['title'] = esc_attr( $attachment->post_title );
return $attr;
}
@caralgar
caralgar / functions.php
Created November 23, 2015 11:29
Remove youtube controls, info & related videos
<?php
// Based on Hide YouTube Related Videos plugin for WordPress
// Ocultar videos relacionados en youtube
add_filter('oembed_result', 'hide_youtube_related_videos', 10, 3);
function hide_youtube_related_videos($data, $url, $args = array()) {
//Setup the string to inject into the url
$str_to_add = apply_filters("hyrv_extra_querystring_parameters", "wmode=transparent&amp;") . 'rel=0';
@caralgar
caralgar / functions.php
Created December 3, 2015 08:25
How to add custom menu to dashboard (or remove)
<?php
/*
Reference @codex:
- Add menu: https://codex.wordpress.org/Function_Reference/add_menu_page
- Select icon: https://developer.wordpress.org/resource/dashicons/#menu
- Remove menu: https://codex.wordpress.org/Function_Reference/remove_menu_page
- Remove sub-menu: https://codex.wordpress.org/remove_submenu_page
@caralgar
caralgar / anypage.php
Last active December 8, 2015 00:55
Get permalink WPML for any content
<?php
/*
How to get the permalink for any content
- Reference: https://wpml.org/wpml-hook/wpml_element_link/
- More hooks & examples at: https://wpml.org/documentation/support/wpml-coding-api/wpml-hooks-reference/#hook-605226
*/
// apply_filters( 'wpml_element_link', int $element_id, string $element_type, string $link_text, array $optional_parameters, string $anchor, bool $echo, bool $return_original_if_missing )
apply_filters( 'wpml_element_link', 1, 'page');