Created
June 27, 2023 22:24
-
-
Save vicctim/f8c78d9a17acd7aa1ea982f8dd451f99 to your computer and use it in GitHub Desktop.
[WordPress] Função para alterar o valor do campo metafield
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <? | |
| // Função para alterar o valor do campo metafield | |
| function alterar_valor_metafield($atts) { | |
| // Extrai os atributos do shortcode | |
| $atts = shortcode_atts( | |
| array( | |
| 'post_id' => 0, // ID do post | |
| 'metafield' => '', // Nome do campo metafield | |
| 'valor_on' => 'on', // Valor quando o switcher está ativado | |
| 'valor_off' => 'off' // Valor quando o switcher está desativado | |
| ), | |
| $atts, | |
| 'switcher' | |
| ); | |
| // Verifica se o ID do post é fornecido | |
| if ($atts['post_id'] == 0) { | |
| return 'É necessário fornecer um ID de post válido.'; | |
| } | |
| // Obtém o valor atual do campo metafield | |
| $current_value = get_post_meta($atts['post_id'], $atts['metafield'], true); | |
| // Alterna o valor do campo metafield | |
| if ($current_value === $atts['valor_on']) { | |
| update_post_meta($atts['post_id'], $atts['metafield'], $atts['valor_off']); | |
| $new_value = $atts['valor_off']; | |
| } else { | |
| update_post_meta($atts['post_id'], $atts['metafield'], $atts['valor_on']); | |
| $new_value = $atts['valor_on']; | |
| } | |
| // Retorna a mensagem de sucesso com o novo valor do campo metafield | |
| return 'Valor do campo ' . $atts['metafield'] . ' alterado para: ' . $new_value; | |
| } | |
| add_shortcode('switcher', 'alterar_valor_metafield'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
[switcher post_id="post_x" metafield="_valor_y"]