You would register your ajax-rendering code with the action wp_ajax_{my_action_goes_here}
for logged-in users, or wp_ajax_nopriv_{my_action_goes_here}
for users not logged in. Something like:
add_action("wp_ajax_nopriv_get_top_4_posts", function() {
$q = new WP_Query(array(
"posts_per_page" => 4
));
exit(json_encode($q->posts));
});
This would go in a theme functions file or a plugin of your choice (any location that is evaluated before templates are pulled in). Then, your ajax would call out to the url admin_url("admin-ajax.php")
and you would pass in an action
parameter of “get_top_4_posts” (or whatever action name you end up using).
Related Posts:
- Get a list of all WordPress.org Plugins?
- How do I make my plugin load before the headers are output so I can redirect wordpress?
- Sell one unique item with Woocommerce? [closed]
- Make thumbnails in woocommerce replace the main image instead of opening fancybox
- Why can I not use a class constant as text domain?
- Function to Set Product type in Woocommerce [closed]
- Deactivate plugins using Migrate DB’s find and replace options
- Trying to get logged-in user data inside php class
- Check if a class exists within a method
- Admin-ajax.php is dying “0” without processing function
- How to remove an Ajax action
- WordPress Plugin Boilerplate – add_action hook in static “activate” function
- add function to saving change on Options Pages
- How can I pass a variable to a page with a SEF url?
- Opendir and WordPress Path
- Multiple files in a plugin
- Gutenberg Blocks – Change EDIT part of the block using editor.BlockEdit filters. How to change the markup?
- Remove rewrite rules generated by plugin during deactivation
- How to show Recent Page instead of Post?
- Allow HTML in Excerpts in Elementor
- Problem In Advance search Plugin [closed]
- Front end inline editing?
- Know which plugin is calling JS in wp_head()?
- How do I disable an update for a specific plugin?
- Security and Must Use Plugins
- echo statement displayed on front end
- Editing wp-config.php
- Change the layout of action links under the plugin names
- Store plugin page content in wp_options?
- HTTPS connection with wampserver for “Facebook for WordPress” plugin
- Each User has a Personal Blog
- looking for a multi-user hello-world plugin
- product available stock day by day
- How to search post titles with whole words only, but not the exact word only?
- Can I disable xml-rpc by setting it to false?
- Add post attachment images their links
- WordPress Admin login redirect to homepage
- Get and Update Most Meta Value as an array in HTML form
- How to upload PDF from Front-End and post automatically?
- WordPress error after installing plugin “Internal Server Error” [closed]
- Template conflict with WC Payment Method
- wp_insert_post() post_author and return post_id are not working
- Different email notifications (about pending posts) to different users
- Multiple Checkbox
- building custom simple shopping cart using wordpress plugin
- Posting code inside the post instead of in the template file using shortcode
- overwrite a plugin function in functions.php
- How to list the categories by custom taxonomy created?
- Allow users to enter and edit data in one-to-many configuration
- How to make is_admin() return false when I need it to?
- Facebook comment into my blog
- Distribute post content through several divs [closed]
- How to create a “plugin” or “block” that can manipulate WordPress DOM in frontend?
- How to rename files during upload to a random string?
- Redirecting to page on form submit – Revue plugin
- Create WordPress category dynamically
- Take Elementor For Email FIeld Check if user is registered. IF not registered then register on woocommerece
- Template selector for woocommerce product
- How to access values from your own settings page in javascript?
- WP Donation Form with custom payment API
- Visual Composer Tabs don’t work on mobile
- How to load plugin static data from cloudfront
- WP-DBManager Plugin Can’t Find Mysql Path
- Subtract Using GravityWP Count Plugin
- Block Missing from Inserter When Following Block Tutorial
- How to Replicate Elementor Licensing Model
- How do I save meta key value in the save function?
- Showing Search Engine Results in Elementor [closed]
- Real estate schema markup not showing up on Rich Results test
- qTranslate‑X is not translating all shortcodes
- How do I call an action hook into wp_ajax hook callback function
- WordPress Plugin and other pages not opening
- I want to display data last 7 days with date wise in chartjs?
- Plugin Icon does not work correctly
- Redirect a specific User role not admins automatically to view the post that they just updated
- Rewrite Rule in htaccess convert query string into slashes
- How can I view forums from different sites in a single panel? [duplicate]
- Manually update notification of plugins needing updating
- The Best Multilanguage Plugin supporting the FSE [closed]
- warning wp session
- Change social icon in twenty twenty three theme
- How to create a buddypress group automatically if a topic is created
- How to add portfolio category post count in main navigation menu?
- Convert from Portfolio post to WooCommerce products [closed]
- /wp-admin/plugins.php takes ages to load, and then 404s
- How to mirror or create a post system into a plugin page
- Problem with a shortcode generating a error [closed]
- Can’t update plugins in WordPress, system returns -1
- Per-Day booking with customer-supplied start time using Woocommerce Bookings
- WP FULL CALENDAR (NO NEXT ARTICLES DISPLAY)? [closed]
- Redux Framework slides field – How Can i add new placeholder Filed? [closed]
- Woocommerce business card flyer design [closed]
- Wp private content show me files attachments into post
- How can I make a function work only for desktops and not for mobiles?
- remover item do carrinho na página de checkout
- Changing the “OT Process” Plugin’s Default Slug of /process/
- What’s the best way to update my WordPress theme to Elementor?
- How can I make a function work differently for desktops and for mobiles?
- Why my widget is not visible in the WordPress theme Twenty Twenty-Three
- How to create specific wordpress Table