I’m new to WordPress. I have a website that I coded myself. One of the pages has PHP functions that take a google calendar feed (with hard-coded login info) and displays it as needed.
I am now building a new site on wordpress where I want to use the same code. My reading says I should handle this as creating a new plugin so that I don’t overwrite it with any theme updates.
My question: how can I simply copy my PHP code into a file in it’s own plugin folder and then use a shortcode to access it like an include? Specifically, I don’t understand if I am able to use shortcode as an equivalent of <php? include="myscript.php"?>
.
Adding a new shortcode requires the following function in your plugin file:
This means that your plugin has a function named
output_calendar
that is called anytime WordPress sees the shortcode[google-cal]
in post or page content. Put your existing code in theoutput_calendar
function.You only need three things in your plugin:
1) The comment block that identifies your plugin.
2) The
add_shortcode
function call.3) The
output_calendar
function definition.Put the plugin php file in wp-content/plugins (in its own folder or at the root there) and activate it in the admin UI. Add the shortcode to a post or page and see it appear in your site.
Read up on the shortcode docs to learn how to pass parameters to your shortcode function.
Note that if you just want the content to appear in the template outside of post or page content, either put the
output_calendar
function itself in the template files at the location where you want the calendar to appear or use thedo_shortcode
function.