WordPress.org

Ready to get started?Download WordPress

Forums

need some help with %postname% issue (24 posts)

  1. zeroclickmarketing
    Member
    Posted 8 months ago #

    Ok I know this could be one of 2 things really but I can't find the answers. I need to do this here are some examples not a real site i run.

    http://website.com/wordpress-is-complicated

    to instead read

    http://website.com/wordpressiscomplicated

    Is there a way to do this through coding or even make it so that the permalink to begin with does this from the start and just leaves these dashes out of the picture?

    anyone have an idea. I mean there has to be a way to make these look like regular website urls.

  2. gerald@WPcustoms
    Member
    Posted 8 months ago #

    right below to post title on page edit is the permalink which can be edited.
    it can be adjusted there after the title has been set.

    But generally I would avoid this. Searchengines prefer dashes instead of one long word as long as you don`t set a full sentence as the permalink.
    As far as I remember max. 4-5 dashes per url is ok.

  3. zeroclickmarketing
    Member
    Posted 8 months ago #

    Not what I'm looking for I know how to use the regular wordpress. I want this to be done automatically without editing.

  4. gerald@WPcustoms
    Member
    Posted 8 months ago #

  5. zeroclickmarketing
    Member
    Posted 8 months ago #

    I would of thought that but I made that slug page and it did nothing. Unless he's just not very clear what files get what and only posted brief to where someone would need to know how to code well to do it.

  6. zeroclickmarketing
    Member
    Posted 8 months ago #

    I mean they put something in there coding somewhere so it does it automatically. There has to be a way to reverse the coding they did to make it look like a normal website url.

  7. bcworkz
    Member
    Posted 8 months ago #

    The code posted in gerald's link works as advertised, at least on my installation, perhaps you misunderstood how to use this?

    (Disregard the .htaccess post, not sure what that's about, it appears to be a misdirected response.)

    The code is a very simple plugin. You could also add the code to a child theme's functions.php or add it to an existing plugin code you've authored.

    To use it as a separate plugin as the author suggested, create a folder in your plugins folder called "compact-slug". In that folder, create a new file named "compact-slug.php". Place the posted code in this new file. To avoid "unexpected output" warning messages, delete everything below the add_filter(... line.

    Log on to your site as an administrator and activate the "Compact Slug" plugin. All new post slugs will not have dashes nor spaces.

  8. zeroclickmarketing
    Member
    Posted 8 months ago #

    Worked like a charm is there any way to get the actual page title to have no spaces once posted as well. This way anyone else makes a post the title will appear without any spaces?

  9. zeroclickmarketing
    Member
    Posted 8 months ago #

    sorry I mean post title not page title in the above comments.

  10. bcworkz
    Member
    Posted 8 months ago #

    You can add this code to where ever you put the other code:

    add_filter('wp_insert_post_data', function ( $data ) {
    	$data['post_title'] = generateSlug( $data['post_title']);
    	return $data;
    });

    If you still want a mixed case title, you'll need another version of generateSlug() that does not use the strtolower($slug) function.

  11. zeroclickmarketing
    Member
    Posted 8 months ago #

    I will try adding it. I just want the url and pot title to both not have a dash or space between them at all. Just one long word.

  12. zeroclickmarketing
    Member
    Posted 8 months ago #

    Parse error: syntax error, unexpected T_FUNCTION in my compact-slug.php file.

  13. zeroclickmarketing
    Member
    Posted 8 months ago #

    Guess what I mean is the url is fixed perfect now how to fix the post title itself

    Adams Garage = How it is now.

    AdamsGarage = How I want the post title to show as.

  14. bcworkz
    Member
    Posted 8 months ago #

    Unexpected T_FUNCTION means there's a missing parenthesis, bracket, semi-colon, etc. somewhere at or above the location specified. I did test the snippet before posting it, and once again just now. It ran without error each time.

    My above snippet as is will result in a title of "adamsgarage" using your example, all lowercase, no spaces. This occurs when the post is published or updated. As mentioned above, if you want the title to be CamelCase, you need to copy the generateSlug() function definition, rename it, remove the $slug = strtolower($slug); line, and call the renamed revised function instead of generateSlug().

  15. zeroclickmarketing
    Member
    Posted 8 months ago #

    where is this going and how does the final code look?

    I'm very new to wordpress and have no clue where to add this coding and what is going away. Is it a new file or something?

  16. zeroclickmarketing
    Member
    Posted 8 months ago #

    am i adding your code just anywhere to the compact-slug file? or was there a set place it needed to go and replace something there.

  17. zeroclickmarketing
    Member
    Posted 8 months ago #

    <?php
    /*
    Plugin Name: Compact Slug
    Description: This plugin adds a function that condenses spaces in a slug instead of replacing them with a dash.
    Version: 1.0
    Author: Shaun Flagg
    License: GPL2
    */
    
    function generateSlug($slug)
    {
    $slug = strtolower($slug);
    
    $slug = preg_replace('/&.+?;/', '', $slug); // kill entities
    $slug = str_replace('.', '-', $slug);
    $slug = preg_replace('/[^%a-z0-9_-]/', '', $slug);
    $slug = preg_replace('/\s/', '', $slug);
    $slug = preg_replace('|-+|', '-', $slug);
    $slug = trim($slug, '-');
    
    return $slug;
    }
    remove_filter('sanitize_title', 'sanitize_title_with_dashes');
    add_filter('sanitize_title','generateSlug');
    add_filter('wp_insert_post_data', function ( $data ) {
    	$data['post_title'] = generateSlug( $data['post_title']);
    	return $data;
    });
  18. bcworkz
    Member
    Posted 8 months ago #

    My apologies for not being more clear. I should know better, similar questions come up all the time.

    Adding to the bottom of the plugin code page would be fine and is what I had in mind. What you posted should work perfectly with no T_FUNCTION error.

    Just so you know, most code hacks of this nature can typically go in one of two places, a plugin or your theme's functions.php file. Many hackers have a special "catch-all" plugin in which they keep all their random hacks. To save hacks in a theme's functions.php file, one should create a child theme so the alterations do not get lost when the theme is updated.

    Generally speaking, code snippets can go anywhere in a file, but you must be cognizant of code structures and not place snippets inside of such structures. By structures I mean mainly the <?php ?> delimiters. Code not inside of such delimiters is treated as HTML and not executed as code.

    Inside of the PHP delimiters, be careful not to insert snippets inside of other structures such as function definitions, class definitions; if/else, do/while, select/case, and other such blocks, etc.

    You could even place snippets in a separate plugin file (but not separate theme files unless a template). If so, that file must be included or required on the main plugin file.

    PHP is very flexible with code order, you can call a function before it is defined. It does expect a certain organization, preventing insertion of code just anywhere, but within reason code can go almost anywhere.

  19. zeroclickmarketing
    Member
    Posted 8 months ago #

    What i posted gives me a white page of an error

  20. zeroclickmarketing
    Member
    Posted 8 months ago #

    Parse error: syntax error, unexpected T_FUNCTION in /home/content/58/10782558/html/wp-content/plugins/compact-slug/compact-slug.php on line 25

  21. bcworkz
    Member
    Posted 8 months ago #

    I think I know what's going on, you have an older version of PHP that doesn't support closures. Support is somewhat recent, so it doesn't need to be that old. This non-closure version should work for you.

    add_filter('wp_insert_post_data', 'generateTitle');
    function generateTitle( $data ) {
    	$data['post_title'] = generateSlug( $data['post_title']);
    	return $data;
    }

    FYI, notice the only difference between the versions is the use of a function name instead of directly passing the function definition anonymously to add_filter(). Thus, you may see closures referred to as anonymous functions. The advantage of closures, besides not cluttering up the namespace with single use functions, is under some conditions the scope of variables is extended, removing the need to pass added parameters or declare globals.

  22. zeroclickmarketing
    Member
    Posted 8 months ago #

    Did the job of fixing it but I did notice one thing. I was wondering if theres a way to keep it to the posts only still. I originally wanted posts only to be done this way and its doing it to the actual pages as well.

  23. zeroclickmarketing
    Member
    Posted 8 months ago #

    Nevermind if I just turn the plugin off when updating a page it leaves spaces int he title words and stuff. Not a big deal. Thank you for all your help everyone.

  24. bcworkz
    Member
    Posted 8 months ago #

    Easy when you know what to do...
    if ('post' == $data['post_type']) $data['post_title'] = generateSlug( $data['post_title']);

Reply

You must log in to post.

About this Topic

Tags

No tags yet.