How to get data from WordPress $wpdb into React Gutenberg Blocks Frontend?

I am using $ wpdb to do a mysql query and get the data from a php file.

$  results = $  wpdb->get_results("SELECT * FROM table_name");  

Now, I have registered a Gutenberg block with the wordpress api..

the component is a .js file, which uses (WordPress React) under the hood:

import { registerBlockType } from "@wordpress/blocks"; import { __ } from "@wordpress/i18n"; import edit from "./pruefungenEdit";  registerBlockType("mytheme-blocks/pruefungen", {     title: __("Pruefungen Block", "mytheme-blocks"),     description: __("Display a Table of exams", "mytheme-blocks"),     category: "mytheme-category",     icon: {         background: "#f03",         foreground: "#fff",         src: "dashicons-database",     },     keywords: [__("pruefungenBlock", "mytheme-blocks")],     edit: edit,     save: () => {         return <p>Test</p>;     }, }); 

This is my React component that I use for above edit() function:

import { Component } from "@wordpress/element"; import { withSelect } from "@wordpress/data"; import { __ } from "@wordpress/i18n";  class PruefungenEdit extends Component {      render() {         //where to get the data from the query in .php file??         return <div>Data...</div>;     } }  export default PruefungenEdit; 

I can’t use php code obviously inside my .js react file.. I thought about setting up a small nodejs server that queries my localhost mysql table, and send that data via custom api route. THen I query that route on my react frontend and display the data… but that looks like too much workaround to me, I mean php is itself some kind of backend, does WordPress not offer that possibility to query mysql data and send it to the frontend somehow?

My question now is, how do I get the data from the php file into my gutenberg Block that I registered for the block editor frontend?

WordPress front-end user create list of posts wether logged in or not

I am in need of some advice/nudging in the right direction to achieve the following:

Users visiting the WordPress site will be presented with a list of artist profiles (these are set up as a custom post type).

The User will then be able to add/save the post to either a new list or add to an existing list called ‘User Projects’.

If they’re not logged in, they would be able to create a temporary list, with the ability to login and save it.

I have ACF Pro installed, and at the moment I’m using the ‘Post Object’ field to link the posts in the WP Admin, but I now need this to work on the front-end and without the need to be logged in to begin with.

Ideally this would all be done with an ajax button, allowing for a smooth user experience.

All help greatly appreciated.


Imported posts and terms but terms not appearing on the frontend

I created a giant import that grabbed both taxonomy terms and posts from a different application. To import the terms I used wp_set_post_terms( $ post_id, $ term_ids, 'my_taxonomy' ); which works because I see them on the edit screen for each post.

However, when I go on the frontend and look at my post the terms are empty. If I go back on the edit screen and just click on "Save" then they finally appear on the front end.

I have close to 2,000 records and doing this manually would be quite unwieldy and I can’t for the life of me figure out what happened! How do I force this update across the board programmatically?

I’ve tried using both clean_term_cache(); and clean_object_term_cache(); and it didn’t seem to make a difference. Maybe I’m calling these wrong but it’s not doing a thing as far as I can tell.

I’ve tried:

  • clean_term_cache
  • clean_object_term_cache
  • delete_option($ tax.’-slug_children’);
  • delete_option($ tax.’_children’);
  • clean_taxonomy_cache($ tax);
  • wp_cache_flush();

Run Python Functions in frontend [closed]

I know front-end developement with React.js. I want to make a basic web-app where I use some python functions (for example on onClick of buttons etc). These functions would mainly revolve around machine learning (but wont be too complex).

Is there any way to run python functions from React.js apart from ajax queries?

If not, which Python framework should I use to do web development in Python such that I can directly run Python functions as well? I have come across names like Django, Tinkter and Flask but couldn’t figure out what exactly would suit my requirement.

Where does elementor stores the data from the content that is created from the frontend view? [closed]

I am trying to change a value/text in an inner section created by elementor page builder plugin that exists on around 400 pages. I have edited the database (wp_posts) with the right values but it’s not changed on the front end. The values are still the old values on the pages. It almost seems like something is being cached somewhere, but I do not have any caching currently enabled.

If I completely disable elementor and elementor pro. The correct values are shown. If I re-enable elementor the old incorrect values are shown again.

Does anyone know where does elementor stores the data from the content that is created from the frontend view? In the database (where?) in files? (where?) Thanks so much!!

Limit custom post type to the authors only on front-end!

I’m trying to limit the custom-post-types to their authors excluding administers on the homepage. This is how the code looks like on the homepage.php

  <?php             $  args = array(           'post_type' => 'project',           'post_status' => 'publish'           );            $  post_query = new WP_Query ( $  args ); ?>            <?php while($  post_query->have_posts()):$  post_query->the_post(); ?>       //display post type content            <?php endwhile; ?> <?php wp_reset_postdata(); ?> 

I tried using the code below, that almost achieved what i wanted but it is giving an unusual 404 error instead of the homepage for the new users(authors) but works absolutely fine when logged-in as administrator or from old author’s profile before adding this code.

// limit post display to post authors function limit_post_to_only_author($  query) {     global $  current_user;     if (!current_user_can('manage_options')) {         $  query->set('author', $  current_user->ID);     } } add_action('pre_get_posts', 'limit_post_to_only_author'); 

I’ve posted a question with detailed explanation about the issue that the above code is causing here. but would prefer a solution to limit the post to authors on the frontend…

How to do the simplest possible frontend ajax call from a plugin?

I’m trying to make this as simple as possible just to get the basics down, but I keep getting a 400 error and 0 response from admin-ajax.php. I just want to hit ajax with some data. Here’s my js:

jQuery(document).ready(function($  ) {   $  ('body').click(function(e){       $  .ajax({           action: 'the_ajax_hook',           data: 'field=data',           type: 'post',           url: the_ajax_script.ajaxurl,           success: function(response_from_the_action_function) {               $  ("#site-content").html(response_from_the_action_function);           }       });   }); }); 

And here’s my plugin:

function load_my_scripts(){      wp_enqueue_script( 'my-ajax-handle', plugin_dir_url( __FILE__ ) . 'ajax.js', array( 'jquery' ) );      wp_localize_script( 'my-ajax-handle', 'the_ajax_script', array( 'ajaxurl' => admin_url( 'admin-ajax.php' ) ) ); }  add_action( 'wp_enqueue_scripts', 'load_my_scripts' ); add_action( 'wp_ajax_the_ajax_hook', 'the_action_function' ); add_action( 'wp_ajax_nopriv_the_ajax_hook', 'the_action_function' );   function the_action_function(){   echo "field is " . $  _POST['field'];   die(); } 

The localization seems to work because it hits the right URL for admin_ajax, but it’s a bad request and returns nothing.

I know I’m missing something obvious but that’s the thing about obvious mistakes, they don’t seem obvious until someone else shows it to you. What am I missing?

replace featured image from frontend

I need to replace the featured image of a post in the frontend. I’am using this code without success:

<form id="featured_upload" method="post" action="#" enctype="multipart/form-data">     <input type="file" name="my_image_upload" id="my_image_upload"  multiple="false" />     <input type="hidden" name="post_id" id="post_id" value="55" />     <?php wp_nonce_field( 'my_image_upload', 'my_image_upload_nonce' ); ?>     <input id="submit_my_image_upload" name="submit_my_image_upload" type="submit" value="Upload" /> </form>        <?php  if (      isset( $  _POST['my_image_upload_nonce'], $  _POST['post_id'] )      && wp_verify_nonce( $  _POST['my_image_upload_nonce'], 'my_image_upload' )     && current_user_can( 'manage_options', $  _POST['post_id'] ) ) {      include('wp-admin/includes/image.php' );     include('wp-admin/includes/file.php' );     include('wp-admin/includes/media.php' );      if (has_post_thumbnail( $  _POST['post_id'] )) {     $  featured_image = wp_get_attachment_image_src(get_post_thumbnail_id( $  post->ID ), 'full' );     }     $  attachment_id = media_handle_upload( 'my_image_upload', $  _POST['post_id'] ); 

What am’I missing?