September 29, 2013

insert </br> after comma with php

Vektor’s Question:

I array text from custom field “black, grey, white” with this code:

<?php global $wp_query; $postid = $wp_query->post->ID; echo get_post_meta($postid, 'colors', true); ?>

I want show me like this:

black</br>
grey</br>
white

It’s possible with PHP? Many thanks

Use str_replace():

echo str_replace(",", "<br />", get_post_meta($postid, 'colors', true));
May 29, 2013

Jquery Select Box not working on dynamically generated elements

Yaar Mallang Jeha’s Question:

I am with a problem. I am using jQuery.SelectBox for the select box and dropdowns.

It is working fine when the elements are loaded with the page load. But its not working when they are loaded by the ajax i.e on dynamicaly generated elements it is not working.

You can check the file here :- http://rvtechnologies.info/brad/jquery.selectBox.js

CSS codes are style declaration and stylesheets, once the element gets added to the DOM they will be loaded or applied.

Check the name of id, classes and attributes of the generated elements using tools like firebug and see the generated markup.

March 18, 2013

Add php within a string

Question by Brown

I want to take

$imgsrc = get_template_directory_uri() . '/s.php?strings=';

and add the_content_rss('', TRUE, '', 4) after strings.

How do I go about doing that?

$imgsrc = get_template_directory_uri() . '/titles.php?strings=' . the_content_rss('', TRUE, '', 4);

doesn’t seem to output a single URL.

Answer by Starx

First thing that you should understand is

String concatenation requires functions that return value not output/echo them.

So, make sure get_template_directory_uri() and the_content_rss('', TRUE, '', 4); returns a value instead of displaying them. If both functions return a value, the URL should be build as expected.

February 28, 2013

Remove white space on HTML/CSS banner?

Question by js111

I have tried every kind of CSS trick I know to remove this white space including cell-padding, margins, padding ect ect.

Any idea how to remove this white space below the “sign up” on this banner?

http://oil.wpengine.com/

enter image description here

Answer by ThinkingStiff

Add vertical-align: top to this <img> in the row below the row with the “sign up” <img>. I did it inline here, but you’ll probably want a class.

<img src="images/OFC_Home_Header_15.png" width="75" height="10" style="vertical-align: top">

enter image description here

Answer by Starx

This is a slicing problem. Fix this image

<img width="75" height="10" alt="" src="images/OFC_Home_Header_15.png">

This solves the problem:

<img width="75" height="10" alt="" src="images/OFC_Home_Header_15.png" style="margin-top:-2px;">
July 22, 2012

php include (cannot get correct path)

Question by Sandro Dzneladze

I have a file that resides in:

/Library/WebServer/Documents/wordpress/wp-content/themes/directorypress/sidebar-left-big.php

I have another file in sub directory:

/Library/WebServer/Documents/wordpress/wp-content/themes/directorypress/template_directorypress/_gallerypage.php

And in _gallerypage.php I have php include:

<?php include('../sidebar-left-big.php'); //left sidebar, category navigation and ads ?>

Error I get:

Warning: include(../sidebar-left-big.php) [function.include]: failed to open stream: No such file or directory in /Library/WebServer/Documents/wordpress/wp-content/themes/directorypress/template_directorypress/_gallerypage.php on line 9

Warning: include() [function.include]: Failed opening '../sidebar-left-big.php' for inclusion (include_path='.:') in /Library/WebServer/Documents/wordpress/wp-content/themes/directorypress/template_directorypress/_gallerypage.php on line 9

It seems to me I’m doing everything correctly.

I thought that maybe problem is that _gallerypage.php is loaded via include in another file, so ../ relative to that leads to error. But error doesn’t say anything as to where it thinks path to sidebar-left-big.php is.

Answer by Jerzy Zawadzki

use include dirname(__FILE__).'/../sidebar-left-big.php';

Answer by Starx

Yes, You are right.

When you include the _gallerypage.php from another file, it does take the path relative to itself. So, you should fix this.

The best way, might be avoid such difficulties. There are number of ways to do this. Like, One would be define a global root in a constant and include every thing, everywhere as per it.

For example:

define("BASE" , "/wordpress"); //Define a base directory

//now whenever you are including and requirng files, include them on the basis of BASE

require(BASE."/admin/.../some.php");
April 27, 2012

PHP not working correctly in WordPress

Question by Alexander Charles

I have made a plain PHP widget to be displayed on a WordPress sidebar. I have successfully made the widget post the data I am hoping to have filled in on the consecutive page. However where it is supposed to be will not fill in, instead it fills in with "<?php echo $_GET[" then after the text box " />". I am hoping that the email first submitted will fill in on the form on the next page. The code that I have for the registration form is part of a greater widget and looks like the following:

<p class="form-email'.$errorVar.'">
      <label for="email">'. __('E-mail', 'profilebuilder') .$errorMark.'</label>
      <input class="text-input" name="email" type="text" id="email" value="<?php echo $_GET["email"]; ?>" /> 
</p><!-- .form-email -->';

Here is a link to the page: http://universityoutfitters.com/testphp/ — the widget is on the bottom left hand side panel.

Additional information:
The code for the widget is as follows:

<form action="http://universityoutfitters.com/sign-up/" method="post">
    Please submit your email address
    Email: <input type="text" name="email" />
<input type="submit" />
</form> 

Answer by Starx

As the comments told above, you have to wrap it correctly with PHP tags

<?php
echo '<p class="form-email'.$errorVar.'">
      <label for="email">'. __('E-mail', 'profilebuilder') .$errorMark.'</label>
      <input class="text-input" name="email" type="text" id="email" value="'.$_GET["email"].'" /> 
</p><!-- .form-email -->';
?>
March 8, 2012

Simple php if !empty-function

Question by Jörg Mayer

I want to simply echo a link (set in a custom field in WordPress), when the corresponding field is not empty.

I tried the following, but the output is missing the link. Only the field as plain text gets printed

<?php $projekt_link = the_field('link');                                    
  if(!empty($projekt_link)){                                              
    echo '<a href="'.$projekt_link->name.'" class="button">Zum Projekt</a>';
  }
?>

Answer by Starx

If you are using the Advance Custom Fields plugin, then the correct syntax is

$projekt_link = get_field('link');

PHP Redirect Within website

Question by baburao113

I’ve been trying this code since 3 days in a row until a certain time (minutes or seconds) but unable to solve the problem.

My target is to redirect visitor to 10 random URLs which are being selected from a text file. The user will see a certain page for a certain time and then redirect to another page again, the number of pages he will be redirected to is complete RANDOM.

PROBLEM:

The problem is the visitor is not being redirected to any other page which is randomly selected from a text file, instead it is just refreshing the page… But I want to redirect him to other pages from the text file.. Hope you guys understood me by now.

EDIT: Found the problem. Actually the $rand_link is having NULL as it’s value.. { [0]=> NULL } Don’t know why…. ANy solution? Checked the ‘BBnormalLinks.txt’ file for it’s permissions and that file is having some links in it for sure because I just checked it..

Thanks,

Here is the CODE:

<?php // Generate Random Nubmers.. 2 ********
            $numbers2 = range(13,70);

            shuffle($numbers2);

            for ($j=0;$j<1;$j++)
            {
            $numbers2[$j];
            }
                $seconds = numbers2[0];

            //////// For Random URL of Site
            $links = file('BBnormalLinks.txt');
        $rand_link = $links[ mt_rand(0, count($links) - 1) ];                   

                header("refresh:". $seconds .";url=". $rand_link); ?>

Answer by Starx

The syntax is correct but some pointers that can cause this are

  1. Some text have been outputted before the header is passed.
  2. The random page, is not being generated, thus ending up refreshing the same page again and again.

I have a very strong feeling, that your $rand_link is returning blank or null.


Update:

After a few discussion, the problem was the evil path again.

$links = file('patotofileBBnormalLinks.txt');

As baburao113, quoted

I had to move that file to wordpress theme folder lol! Problem resolved 🙂

March 2, 2012

How to close iframe from inside iframe?

Question by Luca Frank Guarini

I’ve got a WordPress site where posts are loaded into an iframe.

This is the code that works:

<a class="trick" rel="<?php the_permalink() ?>" href="<?php the_permalink() ?>"><?php the_title(); ?></a>

$(document).ready(function(){

    $.ajaxSetup({cache:false});
    $(".trick").click(function(){
        var post_link = $(this).attr("rel");
        $("#frame").css("display","block");
        $("#frame").attr("url", post_link);
        $("body").css("overflow","hidden");
    });

  });         </script>
<iframe id="frame" frameborder="no" allowtransparency="true" width="100%" height="100%" scrolling="no" src=""></iframe>

Now, how to close this loaded iframe from inside the iframe?

The main page is index.php (main wordpress loop), the content of the iframe is single.php (single post) without header and footer.

Thanks.


This is what i’ve got in single.php

<head>

<script src="http://code.jquery.com/jquery-latest.js"></script>
    <script>
    $(document).ready(function(){
        $("#close").click(function(){
            $('#frame', window.parent.document).remove();

             });

        });

    </script>


</head> 

<body>
<div id="container-single">
    <button id="close" >Close</button>



    <?php if (have_posts()) : while (have_posts()) : the_post(); ?>

        <article <?php post_class('single') ?> id="post-<?php the_ID(); ?>">

            <h1 class="entry-title"><?php the_title(); ?></h1>

            <div class="entry-content">

                <?php the_content(); ?>

                <?php wp_link_pages(array('before' => 'Pages: ', 'next_or_number' => 'number')); ?>

                <?php the_tags( 'Tags: ', ', ', ''); ?>

                <?php include (TEMPLATEPATH . '/_/inc/meta.php' ); ?>

            </div>


        </article>



    <?php endwhile; endif; ?>

    </div>

</body>

Answer by Starx

I know a little trick actually.

Make a function on your parent page

var closeIFrame = function() {
     $('#iframeid').remove();
}

Inside the iframe you want to close call from anywhere you want

parent.closeIFrame();

Tricky, isn’t it?

November 29, 2011

Redirection using PHP footer in Worpress

Question by The Waves

I have a question about timed redirects in PHP – specifically in wordpress.

We have setup a site using a free Woothemes placeholder theme, it is very limited. But that is OK – the site is simple.

After 20 seconds I would like the page to redirect to another URL – would it be possible to insert some code into footer.php to do this? I have found what looks like the right code:

// Redirect with a delay:
header('Refresh: 20; url=http://www.example.org/');

Can this be inserted antwhere in footer.php?

Any input is welcome.

Answer by Starx

Yes, it can be inserted, but in case some HTML is already by the server you will receive an warning such as header already sent by ..... and the redirection will not function as it should.

Instead you can perform such by clearing everything in the output buffer using ob_clear() that is to be printed and then send the redirection header.

Example:

if($casespecial==true) {
    ob_clean(); //make sure nothing is outputed to the browser
    header('Refresh: 20; url=http://www.example.org/'); //now send the header param

    //After wards, you can resume your normal code and output the template as you require
    .
    .
    .
}
...

Please fill the form - I will response as fast as I can!