Articles of php

Database “Migration” for Plugins?

I’m creating a plugin for WordPress. This plugin will need to save data to the database. In other programming frameworks I’ve worked with, there are systems called “database migrations”. These migrations are SQL ALTER TABLE or CREATE TABLE statements/scripts Sometimes abstracted into the programming language of the framework Written and stored in a common way […]

Displaying the last post on static homepage

(It might not be the first time this question is asked, but every time I found someone else asking it elsewhere, the post was very old and the method used wasn’t efficient anymore.) ► I’d like to be able to show the last article of a specific category (let’s say the category is “news”) on […]

Fetch posts from category in custom page template

I have created custom template (Sell items) for pages. I want to display posts from specific category on page using that custom template. That is working perfectly but only if there are at least one post in the category. If there is nothing it will give following error. What might be wrong? Notice: Undefined offset: […]

Will simple function names in a class structure conflict with other plugins?

I implement a class structure in my plugin, for example: class Ethans_Plugin { public function __construct() { add_filter( ‘admin_init’, array( $this, ‘admin’ ), 10, 1 ); add_action( ‘admin_footer’, array( $this, ‘footer’ ), 10, 1 ); } public function admin() { # code here… } public function footer() { # code here… } } When I […]

Count the number of times the search form template gets included on a page

I’ve got a template that has a search form in several locations. Each search form is identical, containing a text input, two radio buttons, two labels associated to the radio buttons, and a submit button. This works fine for the first call to get_search_form(), but on subsequent calls clicking on the labels change the first […]

How to get post URL in the_content filter?

In WordPress, I use a function in functions.php to only not show certain posts(by category) if a user is not signed in: function my_filter( $content ) { $categories = array( ‘news’, ‘opinions’, ‘sports’, ‘other’, ); if ( in_category( $categories ) ) { if ( is_logged_in() ) { return $content; } else { $content = ‘<p>Sorry, […]

Unable to post simple form data in HTML

I created simple contact me form and want to post form data to PHP page where data will be stored. I am unable to figure out where to upload create_form.php file. Blow is my code. <form name=”customer-details” method=”POST” onsubmit=”return formValidation() action=”contact_form.php”> <fieldset> <legend>Personal information:</legend> Name:*<br> <input type=”text” id=”name” name=”name” placeholder=”Your Name”><br> Company Name:<br> <input type=”text” […]

only delete post within query / for each statement (front end)

I am using this in my functions which I can use to delete the current page (custom post type called – bucket) and all it’s children: function delete_post(){ global $post; $deletepostlink= add_query_arg( ‘frontend’, ‘true’, get_delete_post_link( get_the_ID() ) ); if (current_user_can(‘edit_post’, $post->ID)) { echo ‘<span><a class=”post-delete-link” onclick=”return confirm(\’Are you sure to delete?\’)” href=”‘ . $deletepostlink . […]

Hide post if matches current month and year

I have here a query which display if it matches the current month and year published. The second one need to be the reverse one for the first code which display all post except the one that match the current month and year. // CURRENT MONTH AND YEAR ONLY <?php $current_year = date(‘Y’, current_time(‘timestamp’)); $current_month […]

Looping through WP_Post Object

I have a custom WordPress post array that looks like this: Array ( [1] => Array ( [sno] => 1 [post] => WP_Post Object ( [ID] => 1452 [post_author] => 12 [post_date] => 2017-06-04 18:09:47 [post_date_gmt] => 2017-06-05 01:09:47 [post_content] => Content here [post_title] => Title here [post_excerpt] => [post_status] => publish [comment_status] => open […]