How to show text in a page on Wordpress whithin a plugin - wordpress

I am developing a plugin for a website.(Which is my first plugin for Wordpress)
The basic functionality is querying the database and in specific pages show the data from the database with a specific style instead of the content from the pages.
So far I managed to show some text in every specific page.
This is my code after some basic configurations:
global $wpdb;
global $wp_query;
add_action( 'wp', 'check_which_page' );
function check_which_page()
{
$page_type=get_post_type();
$page_id=get_the_ID();
//echo $page_id;
switch($page_id)
{
case 50:technologyPage();break;
case 82:medicalPage();break;
}
}
function technologyPage()
{
return print "Technology";
}
function salesPage()
{
return print "Sales";
}
function medicalPage()
{
return print "Medical";
}
I've read this post, but I couldn't solve my problem.
WordPress replace content of a page (plugin)
I already read the Wordpress documentation but I havent find anything there.
I found myself a solution, using shortcodes.
global $wpdb;
global $wp_query;
add_shortcode( 'sector_page_display', 'check_which_page' );
function check_which_page()
{
$page_type=get_post_type();
$page_id=get_the_ID();
//echo $page_id;
switch($page_id)
{
case 50:technologyPage();break;
case 82:medicalPage();break;
}
}
function technologyPage()
{
return print "Technology";
}
function medicalPage()
{
return print "Medical";
}
See that instead of add_action I changed to add_shortcode
Then on everypage I will use to show info from the database I add
[sector_page_display]
in the page, so it call my method. You can add variables in there if you want.

You'll want to run that code before WordPress has fully loaded.
Try this
global $wpdb;
global $wp_query;
add_action( 'init', 'check_which_page' );
function check_which_page()
{
$page_type=get_post_type();
$page_id=get_the_ID();
//echo $page_id;
switch($page_id)
{
case 50:technologyPage();break;
case 82:medicalPage();break;
}
}
function technologyPage()
{
return print "Technology";
}
function salesPage()
{
return print "Sales";
}
function medicalPage()
{
return print "Medical";
}
I changed the add_action to now run the code when WordPress is being initialized.

Related

How to get post id using add_my_endpoint?

I'm creating AMP pages on my own, without a plugin, and I have one problem that I can't solve.
function h34_endpoints_add_endpoint_pinup()
{
add_rewrite_endpoint('amp', EP_ALL);
}
add_action('init', 'h34_endpoints_add_endpoint_pinup');
add_filter('template_include', 'amp_page_template_pinup', 2);
function amp_page_template_pinup($template)
{
if (get_query_var('amp', false) !== false) {
$template = plugin_dir_path(__FILE__) . 'amp-template.php';
}
return $template;
}
Now in the amp-template.php file ш need to get the post data (if it is a post) but where and how to get the post ID?
global $post; shows nothing
get_the_ID() - doesn't output anything either.
I will be grateful for any help

Override wordpress routing / url rewriting?

I would like to serve different content based on the URL.
I started with a custom page setup via a custom template but I am open to something else.
The main goal is to have one PHP page that can serve different contents programmatically based on the URL.
For example:
https://some-url.com/my-plugin/ -> run my page
https://some-url.com/my-plugin/foo/ -> run my page
https://some-url.com/my-plugin/foo2/abc/ -> run my page
etc...
I have been looking at add_rewrite_rule, add_rewrite_tag, flush_rewrite_rules and WP_Rewrite API but I am getting confused about which one I should use?
I found an example here but I could not make it work, I get 404s, any idea why?:
/*
Plugin Name: Products Plugin
Plugin URI: http://clivern.com/
Description: Register URL rules for our products
Version: 1.0
Author: Clivern
Author URI: http://clivern.com
License: MIT
*/
function products_plugin_activate() {
products_plugin_rules();
flush_rewrite_rules();
}
function products_plugin_deactivate() {
flush_rewrite_rules();
}
function products_plugin_rules() {
add_rewrite_rule('products/?([^/]*)', 'index.php?pagename=products&product_id=$matches[1]', 'top');
}
function products_plugin_query_vars($vars) {
$vars[] = 'product_id';
return $vars;
}
function products_plugin_display() {
$products_page = get_query_var('pagename');
$product_id = get_query_var('product_id');
if ('products' == $products_page && '' == $product_id):
//show all products
exit;
elseif ('products' == $products_page && '' != $product_id):
//show product page
exit;
endif;
}
//register activation function
register_activation_hook(__FILE__, 'products_plugin_activate');
//register deactivation function
register_deactivation_hook(__FILE__, 'products_plugin_deactivate');
//add rewrite rules in case another plugin flushes rules
add_action('init', 'products_plugin_rules');
//add plugin query vars (product_id) to wordpress
add_filter('query_vars', 'products_plugin_query_vars');
//register plugin custom pages display
add_filter('template_redirect', 'products_plugin_display');
First of all, make sure your pretty permalinks are enabled, in this case the option "Plain" in Settings - Permalinks should be unselected:
Select one of these options to enable pretty permalinks
You can check whether pretty permalinks are enabled in the code like so:
function is_enabled_pretty_permalinks() {
return !empty( get_option( 'permalink_structure' ) );
}
if ( is_enabled_pretty_permalinks() ) {
echo 'Pretty permalinks enabled';
}
Next add a new rewrite rule:
function add_my_rewrite_rule() {
$page_slug = 'products'; // slug of the page you want to be shown to
$param = 'do'; // param name you want to handle on the page
add_rewrite_rule('my-plugin/?([^/]*)', 'index.php?pagename=' . $page_slug . '&' . $param . '=$matches[1]', 'top');
}
add_action('init', 'add_my_rewrite_rule');
Add your parameter to query vars so you will be able to handle it on the page:
function add_my_query_vars($vars) {
$vars[] = 'do'; // param name you want to handle on the page
return $vars;
}
add_filter('query_vars', 'add_my_query_vars');
Then you can access your query var do in the page template or in a shortcode, for example:
function my_plugin_shortcode_handler( $atts ){
$do = get_query_var( 'do' );
if ( $do === 'this' ) {
return 'do this';
} else {
return 'do that';
}
}
add_shortcode( 'myshortcode', 'my_plugin_shortcode_handler' );
Then place the shortcode to the content via Gutenberg.
Check out the links:
https://some-url.com/my-plugin/this/ - outputs "do this"
https://some-url.com/my-plugin/that/ - outputs "do that".
This can be solved by using query params. Like you mentioned you have set up custom page via a custom template. You can read the URL and check for the parameters and based on that you can send data from the PHP template page.
e.g,
function cleanTheInput($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
$data = htmlentities($data);
return $data;
}
if (isset($_GET['page_url'])) {
$theUrl = cleanTheInput($_GET['page_url']);
}
if($theUrl == 266)){
// data for https://some-url.com/?page_url=266
}
if($theUrl == 366)){
// data for https://some-url.com/?page_url=366
}

Wordpress - How to set title dynamically based on content?

I try to set custom titles using data from the content. Those are not pages nor posts - plugin (chaturbate) that I'm fixing somehow process urls like /lala-fafa without having any entries in the posts table.
Here is my code
add_filter( 'the_title', 'chfix_alter_title', 9999);
function chfix_alter_title($title, $id=null)
{
global $chfix_values;
return "$chfix_values[Name] $chfix_values[Age]...";
}
add_filter( 'the_content', 'chfix_doit', 9999 );
function chfix_doit($content)
{
global $chfix_names, $chfix_values;
if (!is_admin())
{
foreach($chfix_names as $name)
{
// parsing content
$chfix_values = chfix_get_field($content, $name);
}
}
return $content;
}
The problem is that the_title filter is called AFTER the_content filter. I also tried wpseo_title, widget_title with no luck. Is any possibility to make what I want?

How to create custom url on Wordpress that call a specific function

I would like to have a custom url on my Wordpress such as
http://mywordpress.com/index.php?act=doit
And this will call a specific function.
What is the best practice to do that?
Thanks
You can do something like this :
add_action('init', 'init_url_custom_function');
function init_url_custom_function() {
add_rewrite_rule('^YOUR_CUSTOM_URL/?', 'index.php?is_custom_function=yes', 'top');
add_rewrite_tag('%is_custom_function%', '([^&]+)');
}
add_action( 'wp', 'check_url_custom_function' );
function check_url_custom_function() {
global $is_custom_function;
if (isset($is_custom_function) && $is_custom_function == "yes")
custom_function();
}
function custom_function() {
// do something
die();
}
PS : you need to regenerate your permalink

Wordpress get values outside function

I need get the value of function that use in wordpress , this function add the value in other page of wordpress using the API add_action , i put the script :
<?php
function wplogincontrol()
{
$a=2;
}
if ($a=="2")
{
header"Location:http://www.google.com");
}
add_action('login_head', 'wplogincontrol');
?>
If use this never can do works and i think use global var and use this :
<?php
function wplogincontrol()
{
global $a;
$a=2;
}
global $a;
if ($a=="2")
{
header"Location:http://www.google.com");
}
add_action('login_head', 'wplogincontrol');
?>
Also i try with $GLOBALS['a'];
But never can get works and can´t get the value outside the function , i try this because if use header location inside function and no work me and give error of header , by this i need get the value outside of function and i see works fine for redirect of header
It´s possible get this value ? , Thak´s , Regards
Are you trying to do something like this ?
<?php
function myfunc()
{
$a=2;
return $a;
}
function wplogincontrol(){
$val = myfunc();
if ($val=="2")
{
header"Location: http://www.google.com");
exit;
}
}
add_action('login_head', 'wplogincontrol');
?>

Resources