I use wp_set_object_terms to set terms for user custom taxonomy, this function only works from the plugin main php file, but not from included php file.
for example this works wp-content/plugins/my_lugin/my_plugin.php:
function set_user_term_a()
{
$user_professions = Array('aaa','bbb');
wp_set_object_terms(46, $user_professions, 'user_profession', false);
clean_object_term_cache(46, 'user_profession');
}
add_action('init', 'set_user_term_a');
but if function included from another php it not work
wp-content/plugins/my_lugin/included_file.php:
echo "the file was successfully included"; // ok
function set_user_term_b()
{
$user_professions = Array('aaa','bbb');
wp_set_object_terms(46, $user_professions, 'user_profession', false);
clean_object_term_cache(46, 'user_profession');
}
add_action('init', 'set_user_term_b');
wp-content/plugins/my_lugin/my_plugin.php:
include_once("included_file.php");
Any ideas what wrong?
Related
I am building a custom Gutenberg block an register the editor script as recommended in the block.json file.
"editorScript": "file:./index.js",
Now I would like to pass data from php to Javascript using the wp_add_inline_script() function. However this requires the handle of the script. In the block.json I don't name a handle and there doesn't seem to be a possibility to do it.
Is there something like a default handle? Or is there another way to pass data from my php plugin file to JavaScript (I want to pass the plugin directory path).
Any help is highly appreciated!
Yes, wp_add_inline_script() is used for passing data from PHP to JavaScript, including Gutenberg blocks.
The script handle you are looking for is the name of your block with the / replaced with a -, eg:
block.json
{
"name": "my/blockname",
"editorScript": "file:./index.js",
...
}
my-block.php
function my_blockname_add_inline_script()
{
// Plugin path for my block
$path = plugin_dir_url(__FILE__);
$handle = 'my-blockname'; // name from block.json with a '-' instead of '/'
$data = 'const myBlockPath ="' . $path . '";';
$position = 'before';
wp_add_inline_script($handle, $data, $position);
}
/**
* Registers the block using the metadata loaded from the `block.json` file.
*/
function my_blockname_block_init() {
register_block_type( __DIR__ . '/build' );
// Enqueue script after register_block_type() so script handle is valid
add_action('admin_enqueue_scripts', 'my_blockname_add_inline_script');
add_action('wp_enqueue_scripts', 'my_blockname_add_inline_script');
}
add_action( 'init', 'my_blockname_block_init' );
I have the following code in my plugin file:
// SET UP REWITE RULES FOR LISTING PERMALINKS //
function my_rewrite_tags() {
add_rewrite_tag('%listingId%', '([^&]+)');
}
add_action('init', 'my_rewrite_tags', 10, 0);
function my_rewrite_rules() {
add_rewrite_rule('^listing/([^/]*)/?','index.php?pagename=listing&listingId=$matches[1]','top');
}
add_action('init', 'my_rewrite_rules', 10, 0);
This idea is that I have a page called "Listing" with the permalink "listing" and I want to be able to have the listing's ID number after it (i.e., /listing/12345/)
I then have a shortcode running on the "Listing" page
// SHORTCODE FOR SINGLE LISTING PAGE //
function my_single_listing(){
ob_start();
include('templates/single-listing.php');
return ob_get_clean();
}
add_shortcode('listing','my_single_listing');
...and the first thing it does is try to get that listing ID with the code:
$listingId = $wp_query->query_vars['listingId'];
I've done this with other plugins I've written in the past, but in this case it's decided to not work. In fact, if I enter the code:
print_r($wp_query);
I get absolutely nothing returned from it at all. (All other content on the page is displaying fine though.)
Any ideas?
Your issue with $wp_query being blank might be due to it not being accessed as a global variable. Prefacing it with a global declaration will allow it to access the global query:
global $wp_query;
print_r( $wp_query )
The issue with the listing ID not being picked up has to do with it not being declared as a possible custom query var. WordPress requires you to declare them before it loads them into the global wp_query for the page (presumably for security). $_GET was able to access them since that bypasses WordPress and just uses it with PHP.
function so_71685702_add_query_vars( $query_vars ) {
$query_vars[] = 'listingId';
return $qvars;
}
add_filter( 'query_vars', 'so_71685702_add_query_vars' );
Once you've got that, $wp_query->query_vars( 'listingid' ) should return a value.
Here's the query_vars hook information page, and the get_query_var hook information page which might be useful for further reading - might cover some things you'll run into based on the way you're setting up custom rewrites and query vars.
I am using this function (in Wordpress's functions.php file) to redirect a specific url to a specific php script file:
add_action('init', function() {
$url_path = trim(parse_url(add_query_arg(array()), PHP_URL_PATH), '/');
if ( $url_path === 'manoa' ) {
$load = locate_template('inc/manoa.php', true);
if ($load) {exit();}
}});
(i know i can use rewrite rules, but i rather use that for a few reasons).
it works great. except i can't use get_query_var on that page. any ideas how to fix it?
I think you should use a different add_action rather than init.
Try to use add_action('wp') instead.
I've recently started using Wordpress PODS plugin and I'm having a little trouble displaying some basic content. I can display custom fields content just fine using the following:
<?php
$field_name = get_post_meta( get_the_ID(), ‘my_field_name’, true );
echo $field_name; ?>
However I can't get basic stuff such as the following:
title (which in regular posts it’s just the_title();)
content (which in regular posts it’s just the_content();)
featured image
Can someone please help me figure out how to pull the title, content and featured image from a POD?
pod-type-slug should be replaced by the slug of your pod type. The pod-slug is the slug of the specific pod content you created:
//Pods->display has been deprecated since Pods version 2.0 with no alternative available according to a warning when trying to use this function:
$pod = pods('pod-type-slug', "pod-slug");
$pod->display('title')
$pod->display('post_content')
etc.
You can also use the default WordPress functionality:
$pod = pods('pod-type-slug', "pod-slug");
$row = $pod->row();
//print the pod content:
echo $row->post_content;
get_the_post_thumbnail($row['ID']);
After a frustrating hour of Googling and looking through their examples, there didn't seem to be a single source of how to get a Pod's content or custom field. The answer above helped me a bit, but wasn't well explained.
In your wp-content/plugins folder, create a new folder called podutils and create a php file called PodUtils.php.
Copy/paste this class into that file:
<?php
//Class to get WordPress Pod data:
class PodUtils {
//get WordPress Pod Object:
public static function GetPodObject($podType, $podSlug) {
//for use with Pods WP Plugin (https://pods.io/):
if(function_exists("pods")) {
$pod = pods($podType, $podSlug)->row();
return $pod;
} else {
return false;
}
}
//get the content from a WordPress Pod:
public static function GetPodContent($podType, $podSlug) {
$pod = PodUtils::GetPodObject($podType, $podSlug);
if($pod !== false) {
return $pod["post_content"];
} else {
return false;
}
}
//get a custom field from a WordPress Pod:
public static function GetPodMeta($podType, $podSlug, $metaName, $isSingle = true) {
$pod = PodUtils::GetPodObject($podType, $podSlug);
if($pod !== false) {
return get_post_meta($pod["ID"], $metaName, $isSingle);
} else {
return false;
}
}
}
To use the class with static methods, do so as follows:
Include the PodUtils.php file in the php file you want to use it:
require_once ABSPATH . 'wp-content/plugins/podutils/PodUtils.php';
Get content:
$strPodContent = PodUtils::GetPodContent('pod-type', 'pod-item-slug');
Get meta (custom field you added to a pod type):
$strPodMeta = PodUtils::GetPodMeta('pod-type', 'pod-item-slug', 'custom-meta-name');
This can easily be changed to an object that you instantiate with public functions, or a plugin of its own.
I use wordpress and I have the following question,
I use the URL:
- www.mysite.com/page
- www.mysite.com/cat/page
- www.mysite.com/custompostype/post
The value of permalinks is %category%/%postname%, everything works fine except now I have to use two languages and I want to configure as follows:
www.mysite.com/EN/page
www.mysite.com/ES/cat/page
www.mysite.com/BR/custompostype/post
I want to change the structure of wordpress urls where the first rule is the language and to take it as variable value. Similar to /%lang%/%category%/%postname%/
i used the wp_rewrite and have had no success, any idea?
for now I just found a solution to my problem, which I handled sessions
<?php function init_sessions() {
if (!session_id()) {
session_start();
} } add_action('init', 'init_sessions'); ?> <?php function detecta_idioma() { if($_SESSION["IDIOMA"]["actual"]=="") {
$_SESSION["IDIOMA"]["actual"]="ES";
$_SESSION["IDIOMA"]["abreviatura"]="";
$_SESSION["IDIOMA"]["nombre"]="Español";
$_SESSION["IDIOMA"]["tag"]="ES"; } if($_GET["action"]=="idioma" &&
$_GET["lang"]) { unset($_SESSION["IDIOMA"]);
switch($_GET["lang"]) { case "EN":
$_SESSION["IDIOMA"]["actual"]="EN";
$_SESSION["IDIOMA"]["abreviatura"]="_en";
$_SESSION["IDIOMA"]["nombre"]="Ingles";
$_SESSION["IDIOMA"]["tag"]="EN"; break; case "ES": default:
$_SESSION["IDIOMA"]["actual"]="ES";
$_SESSION["IDIOMA"]["abreviatura"]="";
$_SESSION["IDIOMA"]["nombre"]="Español";
$_SESSION["IDIOMA"]["tag"]="ES"; break;
} } $IDIOMA=array("actual"=>$_SESSION["IDIOMA"]["actual"],"ab"=>$_SESSION["IDIOMA"]["abreviatura"],"nombre"=>$_SESSION["IDIOMA"]["nombre"],"tag"=>$_SESSION["IDIOMA"]["tag"]);
global $wp_rewrite;
$wp_rewrite->set_permalink_structure($_SESSION["IDIOMA"]["tag"].'/%category%/%postname%');
return $IDIOMA; } add_action('init', 'detecta_idioma'); ?>
works well on the route entries, eg www.misitio.com/en/post-demo, t not working with pages or categories or taxonomies, eg: www.misite.com/es/category/, www.site.com/es/page/subpage or www.site.com/br/taxonomy <----- Dont work, shows error 404 page
I can not use the plugin qTranslate and CMS plugin multulengual either they do not work well with the use of plugins Advanced custom fields and custom post types UI.
Thanks