-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcustom-fields.php
83 lines (48 loc) · 1.84 KB
/
custom-fields.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
<?php
/*
Plugin Name: Custom Fields
Description: Ejemplo para trabajar con custom fields.
Author: Rubén Córcoles
Version: 1.0
License: GPLv2 or later
*/
// add custom field for each post
function myplugin_add_custom_field( $content ) {
$calendar = cal_to_jd( CAL_GREGORIAN, date( 'm' ), date( 'd' ), date( 'Y' ) );
$weekday = jddayofweek( $calendar, 1 );
return add_post_meta( get_the_ID(), 'dia', $weekday, true );
}
//add_filter( 'the_content', 'myplugin_add_custom_field' );
// update custom field for each post
function myplugin_update_custom_field( $content ) {
return update_post_meta( get_the_ID(), 'estado', 'muy feliz', 'feliz' );
}
//add_filter( 'the_content', 'myplugin_update_custom_field' );
// delete custom field for each post
function myplugin_delete_custom_field( $content ) {
return delete_post_meta( get_the_ID(), 'dia' );
}
add_filter( 'the_content', 'myplugin_delete_custom_field' );
// display all custom fields for each post
function myplugin_display_all_custom_fields( $content ) {
$custom_fields = '<h3>Custom Fields</h3>';
$all_custom_fields = get_post_custom();
foreach ( $all_custom_fields as $key => $array ) {
foreach ( $array as $value ) {
if ( '_' !== substr( $key, 0, 1 ) )
$custom_fields .= '<div>'. $key .' => '. $value .'</div>';
}
}
return $content . $custom_fields;
}
add_filter( 'the_content', 'myplugin_display_all_custom_fields' );
// display specific custom field for each post
function myplugin_display_specific_custom_field( $content ) {
$current_mood = get_post_meta( get_the_ID(), 'estado', true );
$append_output = '<div>';
$append_output .= esc_html__( 'Estoy ' );
$append_output .= sanitize_text_field( $current_mood );
$append_output .= '</div>';
return $content . $append_output;
}
//add_filter( 'the_content', 'myplugin_display_specific_custom_field' );