WordPress.org

Ready to get started?Download WordPress

Forums

functions.php dead end (5 posts)

  1. thebradmiskell
    Member
    Posted 3 years ago #

    I've tried and tried and tried over months and on several websites to follow directions I've read many places (including the WP Codex) to add a snippet of code to my functions.php file(s) that will adjust the length of excerpts:

    function custom_excerpt_length($length) {
    return 20;
    }
    add_filter('excerpt_length', 'custom_excerpt_length');

    I've examined functions.php files from other sites of mine built on a variety of themes to try to understand how they, well, function. And I've looked through as much explanation regarding functions.php as I can find (including WP Codex docs).

    Yet I never get the code to work and I generally "break" the website, sometimes having to upload a new functions.php file to the theme directory to make the site work.

    I understand that functions.php is a php file and should, I think, begin and end with standard <?php , ?> tags or markup or whatever. But whether I put the excerpt code within these brackets or without, the file returns errors. I've even had the wp-admin lock me out trying to adjust the functions.php per directives from reliable WP pros.

    so

    A) I'd love to know what I'm doing wrong regarding the excerpt code and, more importantly

    B) I'd love directions on where to find thorough documentation about functions.

    I've been customizing WP themes for myself and clients for a few years now and I really feel like mastering functions is something I need to do. I just can't, for the life of me, find documentation that gets me over the hump. And the WP Codex really isn't helping me.

    Thanks, so much, for guidance.

    Brad

  2. vtxyzzy
    Member
    Posted 3 years ago #

    The code snippet you have above looks correct. Whether or not the php tags are required is dictated by the theme - some require them, some do not.

    Look at a working version of functions.php. If it ends with ?>, then they are required. Just add the code you gave, plus the tags, to the end, making sure that there are no blank lines between the old last line and the start of your added code, or after the end of the added code:

    <?php
    function custom_excerpt_length($length) {
       return 20;
    }
    add_filter('excerpt_length', 'custom_excerpt_length');
    ?>

    Also, make sure you are using a plain text editor and save the file as plain text, not a Word document or some such. I find the free editor Notepad++ to be very helpful here.

  3. thebradmiskell
    Member
    Posted 3 years ago #

    Thank you, vtxyzzy,

    My messing around has totally messed up the (work) site where i was experimenting with this. Something similar happened a while back when i was working with a theme writer who ultimately had me switch out the functions.php in my wordpress install (via ftp). we're no longer working together, so i can't ask about it. but i can no longer do UPDATE in site's the wp-admin without getting all kinds of WARNINGS about the header already being loaded...

    I may have to trash the install and create another work site for this website.

    In the meantime, I'm still unsure about your advice above. I only understand so much php and don't know whether the code you gave me, with the php tags, should come after the closing php tag on the functions.php doc (the doc begins and ends with <?php, ?> tags). Would you try it directly after the current ?> or without the tags inside of the current php tags? Am I making any sense?

    And again, if you know of any good documentation about WP Functions and could steer me towards it I would be grateful, as I already am for your help.

    (BTW- I generally do this stuff with Textedit on my mac, though I may have uploaded a new functions.php to the site that had been opened in Dreamweaver, which I rarely use but may have done this time to open the .php doc.)

    Anyway, thanks, much.

  4. vtxyzzy
    Member
    Posted 3 years ago #

    Would you try it directly after the current ?> or without the tags inside of the current php tags? Am I making any sense?

    Either way is fine.

    i can no longer do UPDATE in site's the wp-admin without getting all kinds of WARNINGS about the header already being loaded

    The 'header already sent warning' is usually caused by a blank line or extra spaces in the first file named in the error message. There is a good writeup in the Codex here.

    And again, if you know of any good documentation about WP Functions and could steer me towards it I would be grateful, as I already am for your help

    Are you asking about WordPress functions, or about PHP functions in general? Do you want to know how to write functions, or just how to use them? I don't know of good documentation that would cover all of the areas.

    If you just want to know what WP functions are available, see the Codex here for a good starting point.

    If you want to write functions, I have found the O'Reilly series of books to be excellent. You might want to start with Learning PHP.

  5. thebradmiskell
    Member
    Posted 3 years ago #

    vtxyzzy,

    you rule. clear, correct explanations. i really appreciate the help.

    your help with the WARNINGS messages was especially helpful.

Topic Closed

This topic has been closed to new replies.

About this Topic