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:
- How to view Plugin Manager in Notepad++
- I found this in a plugin. What does it do? is it dangerous?
- How to do I get a list of active plugins on my wordpress blog programmatically?
- How to Display Product specific to a category with WooCommerce Plugin? [closed]
- How to add field for new site-wide option on Network Settings screen?
- Redirection plugin – how to let the editor access the ‘redirection’ menu?
- Is there any debug toolbar that shows whick hooks are called for the current page in WordPress?
- Output in XHTML or HTML 5 for plugins?
- How much should I charge for creating a WordPress plugin? [closed]
- Add Archive Page Template via Plugin
- How to access options of Redux framework in front end
- How to get user-meta from Social Login registered users?
- How can I save repeatable fields with one value inside an array?
- Prevent page slug from interfering with custom post type archive permalink?
- Automatic updates not working/plugins not installing
- append stylesheet via shortcode
- Is it possible to filter the wp_footer() scripts, read the content, and insert them inline?
- How to call bind function in wordpress actions or hooks
- clean wp_options table unused rows
- Disable Other Plugins on my Custom Post Type?
- Creating a plugin that will add options. Where should the options menu pages go?
- shortcodes displaying outside of content area with echo statement, not what I wanted [duplicate]
- How to write one comment and publish on every post using database or plugin?
- ‘wp_login’ action hook not working with wp-login.php file
- Can WordPress Plugin Directory Cache Delay the Upload of Images?
- Hook (or other way) to find out when another plugin is activated / installed
- Issue using wp-load.php in WordPress Plugin Development
- How to change custom post type pemalink Hierarcy
- Calendar Solution: Handling upcoming Dates with Posts?
- Disable woocommerce cookies and delete cart data automatically
- Why required field not working in Country dropdown in WooCommerce –
- WordPress: cleaning media library
- how to insert data in wordpress table usnig jquery ajax
- Hack-Proof OR Security in WordPress — is it real?
- Is wp-app.php or wp-apps.php needed for WordPress?
- How to distinguish if a plugin is not installed or just not active
- JavaScript in a PHP plugin
- Checking the count within a foreach loop
- Remove specific plugins and themes from the Dashboard->Updates page [duplicate]
- WordPress Apply filter in plugin causes 500 internal error
- Meta query for comparing two dates
- Can’t activate Plugin: unexpected T_STRING, expecting T_FUNCTION [closed]
- How to prevent plugins from sniffing/stealing other plugins’ options?
- Is there a plugin for feedback like Kissmetrics available? [closed]
- Mask and Track Outbound Links
- Shortcode does not expand in Facebook like
- How do i show different group of images below a button when the button is clicked on the same page in wordpress? [closed]
- why need theme,if page builder is there in wordpress [closed]
- Is it possible to instantiate a new WC_Cart?
- How to redirect to a page after the form is submitted
- Exporting Custom Taxonomy in plugin
- Posts to have category specific sidebars with the widgets
- Add background image/layout for single page?
- Separate jQuery for Individual Comments?
- Does WordPress check for updates of a plugin via plugins root folder name?
- How to write a new file when new post has been published in WordPress
- Need to add/remove group of options and display them as rows
- Using call_user_func() within add_settings_section() within a Class
- Paypal Framework
- admin bar disappears completely in 2010 theme
- custom wordpress theme and yoast seo injecting text in body
- Save order without sending the invoice / order details woocommerce [closed]
- Fatal error “Call to undefined function is_plugin_active” each time the plugin is activated
- Block content with popup
- How to check if short code is present in template?
- Product Page loads terribly slow when product gallery images are added
- front end editor creation for Restropress plug in – displaying information from a WP admin area, on a different URL
- Clean old users
- How to make WooCommerce multiple filter with custom meta fields
- One WooCommerce Store to multi distributor sites
- Cannot display wordpress thumbnail
- WordPress User profile with Avatar upload on the front end without plugin?
- How to change the width of certain paragraph or header blocks?
- How do I render content from a wp_editor in a plugin setting field?
- Only return taxonomies that are linked to a category & product
- Settings Fields not showing up in Submenu Page
- Multiple CF7 Forms causing multiple recaptcha requests
- Trying to remove a couple categories from shop page
- Enabling plugin on specific pages and subpages
- Widget display in theme
- Can i add custom code in Source in specific page, Header from function.php?
- Plugin to Delete All Posts Monthly
- plugin not working for adding post to page
- Modifying WordPress XML-RPC Built-Ins
- wp_query for more fields plugin
- contact 7 plugin stops some pages from working properly
- Disqus plugin outputs script as literal text
- WordPress “Categories to tag converter” not working on imported Blogger posts
- Shortcode return is printing a 1 afterward
- How we add tabs in plugin page
- Custom booking form field not saving
- Woocommerce Emails not being sent [closed]
- How to register new elementor widget in functions.php
- Foreach retun in shortcode
- Plugin Option Fire Custom Function After Save
- My wp_update_nav_menu action is firing twice
- Display file contents within Plugin
- PHP – Unexpected character in input: ” (ASCII=20) state=0
- Call to undefined function create_function() – PHP 8.2
- What is the best way to override functions of thirdparty plugins?