Add dynamic content to a WordPress Page - wordpress

I want to know the most elegant/best practice way to do the following:
Let's say I have some PHP that returns a string (today's weather, for example). I want to be able to include the string within a WordPress Page, but not by using a plug-in to allow PHP within a Page.
So for example:
This is content of the page that is editable via WordPress pages.
Today's weather is THE_WEATHER. This bit is also editable, but as
an editor I can't edit the PHP that just told us what the weather
is, the developer does that.
What is the best practice way to do this (preferably without using plug-ins)?

Offhand, it sounds like you want to define a shortcode that lets you invoke the PHP function you're talking about and insert its result into a page/post. This pretty much means that you're writing either a (very simple) plugin or you're incorporating the shortcode definition into the functions.php file associated with a theme (which can be a simple child theme of the one you're using).
Which to do probably depends on whether or not you're likely to change themes (if it's just one site) or whether or not you need to do the same thing on different sites that can have different themes. If either is the case, then you should write it as a plugin; otherwise you can just make it part of your theme.
Consult the Codex to learn how to define shortcodes.

Related

Wordpress: How to create frontend table using data from backend DB table

I am relatively new to WP, and am trying to figure out if the following can be done, or if a plugin exists?
I am looking to save data (500K+ rows) into a SQL table (maybe a new table in WP database), and then render the results on a page (frontend) using some basic JS and CSS. Because of the size of data, I am not sure if I should use a plugin that imports from CSV/JSON and would it slow down the site.
Do I have to go all the way and create models and views and then render to an HTML, or is there an easy hack available?
As the last resort, I am also thinking of creating the table part using Flask and SQlite and use it as main domain, and WP blog as subdomain (don't know if it will impact SEO).
Request guidance from experts. TIA
There are plugins to handle table work. Use your favorite internet search engine to find them. You'll have to experiment with various plugins to get one that meets your needs if you go that route.
There two questions here.
One is how to render a table on the front end. If you do this yourself , your best bet is to create a plugin that defines a shortcode. When you write php code for that shortcode's handler, you will include code to read and render the table. You'll probably render it as an ordinary html <table>.
Then, you'll use WordPress's content editor to create a post, or a page, that contains your shortcode. When a visitor views the post or page, your shortcode handler runs and delivers the table you want.
The WordPress plugin API also gives you a way to enqueue css and Javascript to your pages. And, jquery is an inherent part of WordPress's page loads. WordPress also contains most of jqueryUI. That's handy, because there's a really nice jquery-based plugin called Datatables that will get your users some convenient features like pagination and sorting.
The second question is how to load that data into a table. The easy way would be to do it outside WordPress, with a SQL file or some other program to bulk-load it. But if you do it inside WordPress, you'll do it from an administrative page.
If you need to be able to add, update, or delete table entries from the front end, that's going to be some work.
Explaining all this in detail is far outside the scope of a Stack Overflow answer.

WooCommerce custom theme development best practices

I made an e-commerce website before where I copied the template files inside WooCommerce and edited them to display the content how I wanted. But now I know of shortcode and WP_Query which can also be used to get the products / categories etc.
So what I am wondering is which method is best and are there times where you would use one method over another?
There is actually a time and place for different methods. Nix the shortcode though. Generally speaking you shouldn't "hard code" shortcodes into a custom theme (there are exceptions of course), unless you mean that you want to build in a shortcode that theme-users have access to.
That said, if you want to emulate WooCommerce's current style/layout with your own flair, or you want to extend the current templates, use the current template override method. Note this will lead to your theme getting "stale" at some point. WC does a decent job of not changing the templates too often, but it does happen and your theme should accommodate when that happens.
If you're doing something different with a list of products entirely that's not necessarily supposed to look ingrained in the WooCommerce structure, then you can use a custom WP_Query or Database query to accommodate your needs.
So effectively:
If you want to tweak, extend, or reduce the current WooCommerce structure, use template overrides
If you want a fully customized solution or need WC Products in a different capacity, use a custom query method.
Everything woocommerce do well.
No need to use WP_Query separately
If you wants customize the theme you can use add_action() and add_filter()
Also you can modify css.
Hooks Referance:
https://businessbloomer.com/woocommerce-visual-hook-guide-single-product-page/

Set the permalink to any domain in Wordpress

Is this possible to achieve without too much effort or a plugin? It's simply to instantly redirect to a site I've made in my portfolio if the post doesn't have any content.
Note that I don't want a script in the actual post to redirect since I only use Wordpress as the backend and fetch all the data to be used by a JavaScript backbone app.
EDIT : Added some extra info in a comment
Short Answer: No. You cannot set your Post Permalink to an external asset without directly editing the database.
Long Answer: First off, you need to understand the purpose of the Permalink. It isn't just an arbitrary link to somewhere. It's reserved specifically for storing where that specific post lives within the context of your Wordpress installation.
Your post doesn't live on example.com. It has its own ID, and Wordpress has a specific spot set aside for it where it can be seen on YOUR domain. Leave it as such.
Instead, what you should be doing is employing the tools Wordpress gives you to achieve what you want. A Plugin doesn't exist for what you want because - quite frankly - it's a fairly trivial task when approached CORRECTLY and doesn't warrant the use of an external resource.
So what's the correct way?
Use a Custom Field. Name your Custom Field something like 'externalHref' and fill that in with your link to the website you want to point to in case there's no post content.
Then in whatever templates you're using to generate your posts, just check either for the existence of your Custom Field called 'externalHref' or check for the non-existence of Post Content. In either case, instead of generating a link using The Permalink you can simply build a link that points to wherever you need.

WordPress Plugin Development Idea? Is this possible? Am I on the right track?

I'm very new to WP development. I host a website which needs a list of trails (hiking, biking, etc) and I'd like to write a WordPress plugin to do it.
Can someone please tell me if I'm taking the right approach, and if what I'm proposing is possible.
I'd like the site to end up with an auto-generated and filtered index at http://example.com/trail-guide, and the discrete trail info pages at http://example.com/trail-guide/trailname. This data would all be stored in a single database table holding info for each trail, with an admin page for adding, editing, and deleting entries from here.
Is a WP plugin the best way to go about doing this, or should I be looking at something else?
From the way you're describing, your best bet would be to Register A Custom Post Type. This can be done by adding to your existing theme's Functions.php file, or by creating a plugin.
If you don't plan on changing themes, my advice would be to just hardcode everything into your functions.php file. Otherwise, creating a plugin for this particular job would be the safest alternative.
Using this functionality in tandem with Custom Meta Boxes and Custom Taxonomies will allow you to keep everything organized within the Wordpress Framework with your own special data.
This means that these new posts can also be queried at any time through the standard Wordpress Loop or search box.
If you are uncomfortable with writing your own functions to extend your existing framework, you might want to look into some plugins like GD Custom Posts And Taxonomies Tools to manage your own.
Hope this helps.

Wordpress, alternative single post template

For a project of mine I need to define an alternative template for single posts.
To be more specific I need each post to be displayed as usual when the website is browsed but I need to create different single pages reachable from different URL to create a sort of a mini-website for each post.
(I'm actually using the WooCommerce plugin and what I need to do is to create a mini-website for each product. This needs to be something "outside" from the main website, with a complete different graphic template and is going to be reachable through a QR-code).
Hope it makes a bit of sense.
Thanks for your advices and/or suggestions.
Angelo
I think the easiest way to do that is by registering a custom post type for the special posts that get this special "single.php" template. Then, you can simply write a new single template titled post-[custom post_type].php. Any post you register of this type will use that template.
OR...
If you don't actually need them to be posts, it's even easier if you publish them as pages. By default, pages let you assign a specific page template in the edit screen. So you could make any number of custom templates. Just make sure you add the special header:
/* Template Name: Custom Page */
...so WP knows it's a page template.

Resources