Call   0208 3131682   Mon-Fri 9am-6pm
Monthly Archives

May 2017

How to add a form to a shortcode in WordPress (using PHP and Ajax)

By | Ajax, HTML, Javascript, online forms, Wordpress | No Comments

Recently I needed to develop a Shortcode that would display a form, this blog post details what I did. I used Ajax ( which seems the best way to add forms to shortcodes, but I’d welcome any input from other developers on your approach).

So without further ado lets get on with adding a form to a shortcode in WordPress.

We need javascript to handle submission of the form via ajax (this presumes you have jquery available), and handle the response from the server. green_form is the form id.

 

Next we need the php to handle everything else, you could put this in functions.php (or include from functions.php) or put it into a plugin. The shortcode to include the form in our page or post is [shortcode_with_form] 

The code handles:

  • the output of the shortcode
  • the server side processing of the form (I’ve just created a stub in this example ( function do_something_serverside )
  • and the including of javascript file required ( if you’d like to know abit depth on ajax forms in WordPress see my post on that )
 

Hope you find this interesting if you’re looking to add a form to a shortcode.

 

 

 

How to get a users Roles and how to list all WordPress roles in PHP

By | PHP, Programming, Wordpress | One Comment

This code is handy if you want to find out a users role/s. You could expand it and check if a user has a given role or similar.

This code is based on examples from:

https://codex.wordpress.org/Function_Reference/get_userdata

and

https://wordpress.stackexchange.com/questions/1665/getting-a-list-of-currently-available-roles-on-a-wordpress-site

If you did want to check if the user is in a certain group you could do something like this: