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');
?>
Related
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
I have an addition to url e.g. /products/myproduct/?v=iphone-x/transparent/*/Green
So what I need is for wordpress to add the ?v=iphone-x/transparent/*/Green to all links on the page (only '<a href="">'s, no 'img src=""' or others)
I have managed to do that, but it's a little "dirty". Is there any neat function to add the parameter to all links?
The code I have is as follows:
function callback($buffer) {
// modify buffer here, and then return the updated code
$temp = explode('href="', $buffer);
$buffer = $temp[0];
array_shift($temp);
foreach($temp as $t){
$tt = explode('"', $t, 2);
$buffer .= 'href="'.$tt[0].'?v='.$_GET['v'].'"'.$tt[1];
}
return $buffer;
}
function buffer_start() { ob_start("callback"); }
function buffer_end() { ob_end_flush(); }
add_action('wp_head', 'buffer_start');
add_action('wp_footer', 'buffer_end');
One way you can achieve this is to hook into "the_content" filter. By using regexp with preg_replace_callback function you can get decent results.
function add_para( $content ) {
$content = preg_replace_callback(
"/href=(?>'|\")([^\"']+)(?>'|\")/",
function($m) {
print_r($m);
return "href='".$m[1]."/additional-param'";
},
$content);
return $content;
}
add_filter( 'the_content', 'add_para', 0 );
However, you might run into some issues particularly if your content is not formatted probably (extra spaces, missing tags .. etc).
So the alternative is either to us a JS approach (jQuery for example), or using PHP DOM parser like: PHP Simple HTML DOM Parser
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.
PHP getimagesize is not working when is called from a function in function.php.
function.php:
<?php
// Theme Options
require_once(TEMPLATEPATH . '/functions/admin-menu.php');
add_action('wp_head', 'theme_options', 'get_image_size');
function theme_options() {
// Initiate Theme Options
$options = get_option('plugin_options');
// If a logo image was uploaded then remove text from site title
if ($options['logo'] != NULL)
$remove_text = '-9999px';
else
$remove_text = 0;
?><style>
body {
background-color: <?php echo $options['color_scheme']; ?>
}
#header h1 a {
background: url(<?php echo $options['logo']; ?>) no-repeat scroll 0 0;
text-indent: <?php echo $remove_text; ?>;
}
</style><?php
}
function get_image_size() {
list($width, $height, $type, $attr) = getimagesize($options['logo']);
echo "Image width " .$width;
echo "<BR>";
echo "Image height " .$height;
echo "<BR>";
var_dump($width);
var_dump($heigt);
}
$options['logo'] is returning http://localhost/wordpress/wp-content/uploads/2010/12/logo4.png so the image is being displayed.
I also did var_dump to $width and $height but they didn't show up.
Any suggestions?
EDIT: I pasted the full code of functions.php. $options['logo'] works perfectly in the theme_option function so I don't know why it doesn't work in the get_image_size function.
$options['logo'] is undefined in your code. If it is defined outside of your function, it is not by default available inside of your function.
Please enable error reporting using ini_set('display_errors', 1) and error_reporting(E_ALL), when developing. This will make sure any errors are reported.
I you do not see any error messages, turn up error_reporting and display_errors.
$options['logo'] works perfectly in
the theme_option function so I don't
know why it doesn't work in the
get_image_size function.
As #Sjoerd said: $options is not defined in function get_image_size. It is only defined in function theme_options. That's what functions are about, they are a black box that know about their environment only from the arguments they receive. If you want to make the options visible in function get_image_size, you have to Initiate Theme Options in that function as well.
I found out how to fix it (separating the functions into different add_action statements):
add_action('wp_head', 'theme_options');
function theme_options {
...
}
add_action('wp_head', 'get_image_size');
function get_image_size {
...
}
The add_action only allows one function?
What was the problem?
The length of the excerpt in wordpress is 55 words by default.
I can modify this value with the following code:
function new_excerpt_length($length) {
return 20;
}
add_filter('excerpt_length', 'new_excerpt_length');
So, the following call will return just 20 words:
the_excerpt();
But I can't figure out how could I add a parameter to obtain different lengths, so that I could call, for example:
the_excerpt(20);
the_excerpt(34);
Any ideas? Thanks!
uhm, answering me again, the solution was actually quite trivial. it's not possible, as far as i know, to pass a parameter to the function my_excerpt_length() (unless you want to modify the core code of wordpress), but it is possible to use a global variable. so, you can add something like this to your functions.php file:
function my_excerpt_length() {
global $myExcerptLength;
if ($myExcerptLength) {
return $myExcerptLength;
} else {
return 80; //default value
}
}
add_filter('excerpt_length', 'my_excerpt_length');
And then, before calling the excerpt within the loop, you specify a value for $myExcerptLength (don't forget to set it back to 0 if you want to have the default value for the rest of your posts):
<?php
$myExcerptLength=35;
echo get_the_excerpt();
$myExcerptLength=0;
?>
There is no way to do this as far as I have found using the_excerpt().
There is a similar StackOverflow question here.
The only thing I have found to do is write a new function to take the_excerpt()'s place. Put some variation of the code below into functions.php and call limit_content($yourLength) instead of the_excerpt().
function limit_content($content_length = 250, $allowtags = true, $allowedtags = '') {
global $post;
$content = $post->post_content;
$content = apply_filters('the_content', $content);
if (!$allowtags){
$allowedtags .= '<style>';
$content = strip_tags($content, $allowedtags);
}
$wordarray = explode(' ', $content, $content_length + 1);
if(count($wordarray) > $content_length) {
array_pop($wordarray);
array_push($wordarray, '...');
$content = implode(' ', $wordarray);
$content .= "</p>";
}
echo $content;
}
(Function credit: fusedthought.com)
There are also "advanced excerpt" plugins that provide functionality like this you can check into.
Thanks for your answer, thaddeusmt.
I am finally implementing the following solution, which offers a different length depending on the category and a counter ($myCounter is a counter within the loop)
/* Custom length for the_excerpt */
function my_excerpt_length($length) {
global $myCounter;
if (is_home()) {
return 80;
} else if(is_archive()) {
if ($myCounter==1) {
return 60;
} else {
return 25;
}
} else {
return 80;
}
}
add_filter('excerpt_length', 'my_excerpt_length');