Thanks for your help!
]]>Regarding support, i have faced an issue in my code and within 12 hours the issue was resolved and the support team helped me with extra configurations to do in the website to make it more stable.
I recommend this plugin for everyone who needs to integrate the wordpress website to any other source.
]]>When I disable the plugin then the API works fine. This was working fine until today morning though
{
“status”: “error”,
“error”: “Restricted”,
“error_description”: “Sorry, you are not allowed to access REST API.”,
“error_reason”: “With the free plan, only WordPress default endpoints can be authenticated. You can upgrade to the suitable premium plan to securely access the custom built or 3rd-party plugin endpoint.”
}
]]>The only thing I needed to change was the API endpoint since I created a custom endpoint to the route. But the FAQ cleared this up quickly and it works flawlessly. As a small suggestion I’d propose to add this option to the plugin’s settings page for easier access.
]]>I’m trying to get a valid Token from my user. I’m able to generate a token but once generated, it is not a valid token. How can I generate a valid token?
Trying it with plugin validator:
[1] Get User Token from the Token Endpoint:
img: https://gyazo.com/976a531fc1956ea7e6e0797220b03664
[2] Check if token is valid:
img: https://gyazo.com/5034cb952725c2b3c32824b950117405
Trying it with my own fetch:
Step 1 (get token): https://gyazo.com/1e38f08392a6fad48c05c115300b9fe5
Step 2 (post to CTP named dashboard): https://gyazo.com/b704a0bdfe969ca503d44070d9d2c21c
After this step 2 fetch, i’ve also got an error (403): https://gyazo.com/018646563ab0c693890ce48ea7ac8399
Any one with an idea? I’m totally lost… Thanks
]]>A javascript code enables to check rows with an input checkbox. If checked, an ajax call send records ‘ids’ to an endpoint. Here I have a callback function that contains the php code to save data as csv. Everything works, in the sense that checked records ‘ids’ are correctly sent to the endpoint. The callback function grabs the ids and start a $wpdb query. Headers are prepared before echoing anything else and variables (first label row and data) are correctly stored in arrays. However the csv is not created. Here’s my code
Page with entries displayed
<?php
//the full table with rows showing mysql records
?>
<input type="button" id="export_records" class="button-primary" value="Export Selected">
<script>
// export as csv selected records
$('#export_records').on('click', function(e) {
var row_data = [];
$(".chk_id:checked").each(function() {
row_data.push($(this).data('form-id'));
});
if(row_data.length <=0) {
alert("Please select records.");
}
else {
WRN_PROFILE_EXPORT = "Are you sure you want to export "+(row_data.length>1?"these":"this")+" row?";
var checked = confirm(WRN_PROFILE_EXPORT);
if(checked == true) {
var selected_values = JSON.stringify(row_data);
$.ajax({
type: "POST",
url: "<?php echo get_rest_url(null, 'sight-form-tethys/v1/handle_export_entries'); ?>",
contentType: "application/json",
dataType: 'json',
data: selected_values,
success: function(response) {
var form_ids = response.form_ids;
WRN_PROFILE_EXPORTED = "You have exported "+form_ids.length+" rows.";
confirm("You have exported "+form_ids.length+" rows.");
$("#select_count").html(form_ids.length+" Exported");
},
error: function (xhr, status, error) {
console.error(xhr.responseText);
}
});
}
}
});
</script>
The response of the ajax call is a JSON with the checked id (ex. [16,17])
The endpoint and relative callback function is as follows
<?php
public function __construct( $plugin_name, $version ) {
add_action( 'rest_api_init',array( $this,'sigh_form_handle_export_entries' ));
}
public function sigh_form_handle_export_entries(){
// route url: domain.com/wp-json/$namespace/$route
$namespace = 'sight-form-tethys/v1';
$route = 'handle_export_entries';
register_rest_route($namespace, $route, array(
'methods' => 'POST',
'callback' => [$this, 'handle_export_entries_function']
));
}
public function handle_export_entries_function(WP_REST_Request $data){
$result = $data -> get_params();
$result = implode(',', $result);
if ( !empty($result) ) {
// Set headers for CSV file download
header('Pragma: public');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Cache-Control: private', false);
header('Content-Type: application/csv; charset=utf-8');
header('Content-Disposition: attachment; filename="csv_export.csv"' );
header('Content-Transfer-Encoding: binary');
// Create a file pointer connected to the output stream
$output = fopen('php://output', 'a+');
// Retrieve data from your specific table
global $wpdb;
$table_name = $wpdb->prefix . 'sight_form_data'; // Replace 'your_table_name' with the name of your table
$request =$wpdb->prepare("SELECT * FROM $table_name
WHERE id_form
IN ($result)");
$entries = $wpdb->get_results($request, ARRAY_A);
// Write headers to the CSV file
$header_row = [
'First name', 'Last name', 'Country', 'Email', 'Phone', 'Date sighting', 'Time sighting',
'Species', 'Confidence', 'Latitude', 'Longitude', 'N animals', 'Offsprings', 'Sea state',
'Want to be contacted', 'Privacy consent', 'Video url', 'File URL'
];
fputcsv($output, $header_row);
// Write data rows to the CSV file
foreach ($entries as $entry) {
// Prepare an array with values to write to CSV
$csv_data = array(
$entry['first_name'],
$entry['last_name'],
$entry['country'],
$entry['email'],
$entry['phone'],
$entry['date_sighting'],
$entry['time_sighting'],
$entry['species'],
$entry['confidence'],
$entry['latitude'],
$entry['longitude'],
$entry['n_animals'],
$entry['offsprings'],
$entry['sea_state'],
$entry['contact'],
$entry['consent_privacy'],
$entry['video_url'],
$entry['fileUrl'] // Assuming this is the field with file URLs
);
//
fputcsv($output, $csv_data);
}
// Close the file pointer
fclose($output);
$exported_ids = explode(',', $result);
return new WP_REST_Response(array('form_ids' => $exported_ids), 200);
}
else {
return new WP_REST_Response(array('message' => 'Something went wrong. Try again'), 400);
}
}
?>
I checked in various forums, but it seems that the way I built the csv is correct: $csv_data and $header are correct. $output contains ‘Resource id #15’. By inspecting the request and response of the ajax call I have:
request: the JSON data with records ids
response: a long string similar to this
IkZpcnN0IG5hbWUiLCJMYXN0IG5hbWUiLENvdW50cnksRW1haWwsUGhvbmUsIkRhdGUgc2lnaHRpbmciLCJUaW1lIHNpZ2h0aW5nIixTcGVjaWVzLENvbmZpZGVuY2UsTGF0aXR1ZGUsTG9uZ2l0dWRlLCJOIGFuaW1hbHMiLE9mZnNwcmluZ3MsIlNlYSBzdGF0ZSIsIldhbnQgdG8gYmUgY29udGFjdGVkIiwiUHJpdmFjeSBjb25zZW50IiwiVmlkZW8gdXJsIiwiRmlsZSBVUkwiCkVsZW5hLFBvbGl0aSwsLCwwLDAsLCwwLDAsLCwseWVzLCJubyBhZ3JlZSIsLApyZWVydGVlcSxyZXdlcmYsLCwsMCwwLCwsMCwwLCwsLHllcywibm8gYWdyZWUiLCwKeyJmb3JtX2lkcyI6WyIxNiIsIjE3Il19
Can someone help please? Thanks very much
]]>Here the function for the axax call on the admin page
<script>
//check all checkboxes
(function( $ ) {
// Function to handle AJAX requests for deleting and exporting entries
function handleAjax(action, data) {
// Construct the URL for the REST API endpoint
var customEndpointUrl = '/wp-json/sight-form-tethys/v1/' + action;
// Make the AJAX request using fetch
fetch(customEndpointUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'X-WP-Nonce': '<?php echo wp_create_nonce('wp_rest'); ?>'
},
credentials: "same-origin",
body: JSON.stringify({ data: data })
})
.then(response => {
if (response.ok) {
// Handle success response
console.log('Entries ' + action + ' successfully');
} else {
// Handle error response
console.error('Error ' + action + ' entries');
}
})
.catch(error => {
console.error('Error:', error);
});
}
$('#delete_selected').on('click', function() {
var selectedEntries = [];
$('input[type="checkbox"]:checked').each(function() {
var entryId = $(this).attr('name');
selectedEntries.push(entryId);
});
console.log(selectedEntries);
handleAjax('delete_entries_ajax', selectedEntries);
});
})( jQuery );
</script>
and this is the registered endpoint
public function __construct( $plugin_name, $version ) {
$this->plugin_name = $plugin_name;
$this->version = $version;
add_action('admin_menu', array( $this, 'addPluginAdminMenu' ), 9);
add_action('admin_init', array( $this, 'registerAndBuildFields' ));
add_action('admin_notices', array( $this,'sigh_form_tethys_admin_notice'));
add_action('rest_api_init', array( $this, 'register_delete_entries_endpoint'));
}
public function register_delete_entries_endpoint() {
$namespace = 'sight-form-tethys/v1';
$route = 'delete_entries_ajax';
register_rest_route($namespace, $route, array(
'methods' => 'POST',
'callback' => [$this,'delete_entries_rest_api_callback'],
'permission_callback' => [$this, 'delete_entries_rest_api_permissions_check'],
));
}
public function delete_entries_rest_api_permissions_check($request) {
// Perform permission check logic here
return current_user_can('manage_options');
}
public function delete_entries_rest_api_callback($request) {
$selectedEntries = $request->get_param('data');
file_put_contents('somefilename.txt', print_r($selectedEntries, true), FILE_APPEND);
// Perform delete operation for selected entries
// Example: $wpdb->delete($table_name, ['id' => $selectedEntries]);
return new WP_REST_Response(array('message' => 'Entries deleted successfully'), 200);
}
I noticed that after few seconds the error 404 is reported inspecting the Network, I see a series of ajax.php POST containing the id sent (the first, if more than one row is selected) with the following request, like if not finding the endpoint he call is sent to ajax.php
Interval "60"
_nonce "b50b096d6e"
action "heartbeat"
screen_id "sighting-form-tethys_page_sighting-form-tethys-entries"
has_focus "false"
I already console.log all variables in the ajax call and they are correct. I also checked in wp-json that the endpoint created is registered.
What is the problem? Thnaks a lot
]]>I am trying to define a page with the rewrite API like this:
add_action('init', 'register_endpoints');
function register_endpoints(): void
{
add_rewrite_rule('^my-custom-page/?', 'index.php?custom_page=my-custom-page', 'top');
}
add_filter('query_vars', 'add_query_vars');
function add_query_vars(array $vars): array
{
$vars[] = 'custom_page';
return $vars;
}
add_filter('template_include', 'add_pack_template');
function add_pack_template(string $template): string
{
// Check if it's the custom page
if (get_query_var('custom_page') == 'my-custom-page') {
// Set template path
$template_path = __DIR__ . '/partials/my-custom-page.php';
// Check if template file exists
if (file_exists($template_path)) {
return $template_path;
}
}
return $template;
}
And my template looks like this:
<?php /* Template Name: My Custom Page */ ?>
<?php get_header(); ?>
<div id="primary" class="content-area">
<p>Hello world</p>
</div>
<?php get_footer(); ?>
The problem is, I am currently using a block theme, so a warning shows that get_footer() and get_header() aren’t defined, and while my custom contents are displayed on the page, the rest of the page doesn’t maintain the theme and is raw HTML. How can I define my template to work with a block theme?
And also, is this the best way to handle this? Could anyone provide me with tips or where to look at for the best possible way to do this?
Many thanks
]]>