maj general/ACF

This commit is contained in:
sebvtl728
2025-01-06 00:04:43 +01:00
parent e734ca68fc
commit a7c84fe336
5 changed files with 281 additions and 76 deletions
@@ -263,78 +263,3 @@ if ( ! function_exists( 'hello_elementor_body_open' ) ) {
}
}
// Ajouter les champs ACF à l'API REST pour les pages
add_action('rest_api_init', function () {
register_rest_field(
'page',
'acf',
[
'get_callback' => function ($object) {
return get_fields($object['id']);
},
'schema' => null,
]
);
});
// API RANK SEO
add_filter('rest_prepare_page', function ($response, $post) {
$seo_data = [
'rank_math_title' => get_post_meta($post->ID, 'rank_math_title', true),
'rank_math_description' => get_post_meta($post->ID, 'rank_math_description', true),
];
$response->data = array_merge($response->data, $seo_data);
return $response;
}, 10, 2);
add_action('rest_api_init', function () {
register_rest_route('custom/v1', '/contact', array(
'methods' => 'POST',
'callback' => 'handle_contact_form',
'permission_callback' => '__return_true', // Permet l'accès public
));
});
// Formulaire
function register_contact_endpoint() {
register_rest_route('custom/v1', '/contact', [
'methods' => 'POST',
'callback' => 'handle_contact_form',
'permission_callback' => '__return_true',
]);
}
add_action('rest_api_init', 'register_contact_endpoint');
function handle_contact_form($request) {
$params = $request->get_json_params();
$name = sanitize_text_field($params['name'] ?? '');
$email = sanitize_email($params['email'] ?? '');
$subject = sanitize_text_field($params['subject'] ?? '');
$message = sanitize_textarea_field($params['message'] ?? '');
if (empty($name) || empty($email) || empty($subject) || empty($message)) {
return new WP_Error(
'incomplete_fields',
'Tous les champs doivent être remplis.',
['status' => 400]
);
}
// Logique pour envoyer l'e-mail ou enregistrer les données
$to = get_option('admin_email'); // Adresse e-mail de l'administrateur WordPress
$headers = ['Content-Type: text/html; charset=UTF-8', 'From: ' . $name . ' <' . $email . '>'];
$mail_sent = wp_mail($to, $subject, nl2br($message), $headers);
if (!$mail_sent) {
return new WP_Error('email_not_sent', 'Le message na pas pu être envoyé.', ['status' => 500]);
}
return [
'success' => true,
'message' => 'Message envoyé avec succès.',
];
}