You don’t want to add function vote_up() { [...] }
at the top of every post if you display more than one post on a page. You want to declare the vote_up()
function in your plugin’s PHP file.
Once the plugin declares the function, as long as the plugin is active, you can just call the function wherever you need it.
I would however suggest two further changes:
- Prefix the function name.
vote_up
is rather generic, and you run the risk of colliding with other plugins and/or themes. Something like [plugin_name]_vote_up()
would work nicely.
- Check to make sure the function exists before using it.
If you call vote_up
anywhere outside the plugin (e.g. a theme template file), surround the call as follows:
if ( function_exists( 'vote_up' ) ) {
vote_up();
}
This will ensure that your theme files will still function properly if you ever need/want to deactivate the plugin for some reason.
Related Posts:
- “Can’t use function return value in write context” error
- Using Multiple Submit buttons to trigger customised php functions
- A non-numeric value encountered in /wp-includes/functions.php on line 68
- Function won’t run onclick using Ajax
- Create special button on WP Tiny MCE Posts Editor for Shortcodes
- Programmatically revert to backup .htaccess a possibility?
- Can’t log in to WordPress wp-admin after adding code to functions.php
- Cannot find the php_ini configuration file to find my error log to see why my code is not working
- Site title not showing. Please help me
- syntax error unexpected ‘}’ at 364 line
- WordPress causing all code to be displayed on line 1. Receiving multiple errors after cleaning cookies and cache
- WP Debug enabled Undefined index error in a widget
- Need help with Deprecated: Non-static error when update PHP 7.4 -> 8.1 with Dyad 2 theme
- How to set and use global variables? Or why not to use them at all
- How to Add Image to WordPress RSS-Feed with no Plug-in?
- How to remove a metabox from menu editor page?
- Enqueue Javascript Correctly for 3.5
- How to add active class to custom menu using while loop and wp_list_pages
- Cleanest/Fastest way to avoid calling and retrieving data from the database multiple times?
- Add footer.php to WordPress child theme
- Error when requesting password reset email – wp authentication
- WooCommerce – Display variation custom field value [closed]
- Restrict WordPress File Type Uploads by User Type
- New Plugin Review
- Problems with function on function.php
- Woocommerce HTML email option unavailable
- Use template for posts with a particular category grandparent
- How to slow down server response
- Click loads template via ajax
- Randomise upload filenames (or another solution to hide the original image URL from theft?)
- jQuery not defined
- Use menu link or onClick to set a variable
- Automatically remove a canceled order in Woocommerce
- WordPress add post format support not working
- How to remove the cufon script from Dzonia Lite theme [closed]
- How to put “Read more” link in Custom Excerpt inside p tag?
- Pass Category Name, Description and Photo into variables to pass to jQuery
- Change “add to cart” button color based on Woocommerce product category [closed]
- Get users that likes the post
- Create highly customized submenu (possibly using wp_list_pages)
- $wpdb->wp_users returning empty value for
- WordPress Custom field Colors
- php syntax : [ && ] between commands [closed]
- Which PHP page is the Default Posts Page
- meaning of (array)function()
- Need to put a script above tag in header.php – WP 5.7.1
- Sending a custom form data in email through WP Mail Function
- How to call multiple functions from multiple files into a WordPress page template [closed]
- Isotope Drop Down Categories Filter Function
- How to use array in function to get only value I want
- What is the best way to define constant options for a theme?
- How to remap one of the TinyMCE Advanced Editor button to open the wordpress media library?
- How to add PHP code in functions.php wordpress
- add_filter function concatenate string and locate_template function
- How to return a string that has a jQuery and Ajax inside in a shortcode?
- get_page_templates only return templates with “home” in the filename
- Nav-Menu not showing up
- Show different website layout if no sidebar added
- get_template_part function and add images
- Need advice on theme customizer and child themes
- How to make sure relative URL works when site is not on root domain?
- How to pass hook variable to function?
- Migrating site, only header shows
- remove specificly the last tag in all posts
- Let current user know pending posts counts using wp_query
- How to check post meta isset or not empty during publish/save post?
- how to changes mobile menu toggle breakpoint in WordPress
- Get page permalink in contact form
- How to display already created menus via php?
- Grabbing Image name From Product Image Gallery
- How to hide a child category and show his parent category
- Gutenberg block don’t save rich text content
- remove_action from parent theme using child theme functions.php
- Cant get blog name with get_option
- How to change this ajax function to submit to the default wordpress content area instead of the custom field ‘seller notes’?
- Remove span tags from WooCommerce Downloads page
- Trying to get property ‘ID’ and ‘post_author’ of non-object error
- Extra editor on top of default in Custom Post Type
- How do I fix Undefined variable using $_POST in function?
- How to add typehints to function in functions.php when argument is an object?
- Pagination 404 error(I have refreshed the permalinks and tried several previous options)
- How Can I Display the Last Modified Date for User Profiles on WordPress?
- WordPress database error only while loading page for 1st time
- Custom Post type Ajax search results
- Customizing the output of the archive and category widget without altering the original behavior of the widget
- How can I include tags in wordpress search without a plugin
- How to set up an auto delete post?
- Display a post based on its metabox selection
- apply_filters() function
- Overwriting a Shortcode
- Search box background on a different page template
- WP Ecommerce: Display the “Order Notes” as column in the Sales Log [closed]
- A next page function with shortcode?
- What is the wrong with this function to enqueue the scripts and styles to the theme? [closed]
- Warning: Attempt to read property “term_id” on int – Woocommerce
- Warning: filemtime(): stat failed for wp_
- Hide category by tag
- add_rewrite_rule image from /images/site2/favicon.ico to /favico.ico
- Background Tasks in a WP Cronjob?
- Tags being Inserted in html from plugin PHP not passing through the_content