0

I am very new to PHP. As a graphic designer it is hard for us to conceptualize PHP and back-end codes to do what we envision, so please go easy on me. :) It doesn't come naturally to people like me.

I have a block of code that I want to call into my theme in the efforts of minimalizing the front-end.

<title>
<? global $page, $paged; wp_title( '|', true, 'right' ); bloginfo( 'name' );
  $site_description = get_bloginfo( 'description', 'display' );
  if ( $site_description && ( is_home() || is_front_page() ) )
    echo " | $site_description";
  if ( $paged >= 2 || $page >= 2 )
    echo ' | ' . sprintf( __( 'Page %s', 'framework' ), max( $paged, $page ) );?>
</title>

I need to make it cleaner. Example:

<title>
  <? require(TEMPLATEPATH . '/library/clean.php', 'FUNCTION_NAME_TO_CALL')
</title>
  1. Am I able to place the code within directly into a function?
  2. Am I able to require() the clean.php and tell that require() to only use a specific function? If so how?

If my logic in doing this is wrong then please let me know, I am eager to learn.

3 Answers 3

1

You may try something like this:

<title><?php myTitle(); ?></title>

Declare the myTitle() function in your themes_folder/functions.php file like this:

// In functions.php
function myTitle()
{
    // Put the code here...
}

Note: I've use myTitle() as function name but you may use anything as you like.

Sign up to request clarification or add additional context in comments.

5 Comments

1. Am I able to place the code within <title> directly into the function myTitle()? 2. Am I able to require() the clean.php and tell that require() to only use a specific function? If so how?
One last question. In efforts to bullet-proof Wordpress my functions.php file has 10 requires in it that point the library. I do not want Wordpress to be updated and my functions file wiped (has happened previously). How can I still use <? myTitle(); ?> and call the function this way? I hope this makes sense.
If you update the WordPress then the functions.php will remain the same, i run a blog using WordPress and regularly update it without any problem.
That is good to know. This happened to me a few years back, I've been wary every since. Might have been an error of my own, though. Still, for the sake of knowledge on the topic, would I still be able to call this function even if my functions.php require other php files elsewhere in, say, the library directory? Thanks in advance, not trying to be obsessive with this :)
Since this function is inside your functions.php file then it's available everywhere in the script and you may use include/require as well, include/require is just like copy and pasting the code.
1

Start by getting the PHP syntax correct. You need need semi-colon at the end of your require line and then close the PHP code block.

<? require(TEMPLATEPATH . '/library/clean.php'); ?>

Then, as someone mentioned, you can call your specific functions. Assuming clean.php has a function called 'getTitle'...

<title><?php getTitle() ?></title>

3 Comments

For the sake of quickly posting I did not worry about syntax. However in development I use a PHP checker and make sure my HTML and PHP syntax is perfect. :P Sorry for not doing so beforehand, I was only trying to get the concept across.
Alright. I think I jumped on this question thinking you had something setup and it just wasn't working. You were just trying to get the concept.
Correct. Though for future reference I should probably make sure it has correct syntax before posting. Lol. Thank you for your answer.
1

Write the code as a function in the theme's functions.php file. So you put

function cleanTitle() {
   global $page, $paged; wp_title( '|', true, 'right' ); bloginfo( 'name' );
   $site_description = get_bloginfo( 'description', 'display' );
   if ( $site_description && ( is_home() || is_front_page() ) )
      echo " | $site_description";
   if ( $paged >= 2 || $page >= 2 )
      echo ' | ' . sprintf( __( 'Page %s', 'framework' ), max( $paged, $page ) );?>
}

into the functions.php file and <?php cleanTitle() ?> into your page template.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.