{"id":22051295,"url":"https://github.com/widilo/auto-generate-wordpress-post-title","last_synced_at":"2026-05-17T00:33:48.158Z","repository":{"id":265285669,"uuid":"895687197","full_name":"widilo/auto-generate-wordpress-post-title","owner":"widilo","description":"Automatically generates a post title based on the first line of the post content when a post is saved or updated. ","archived":false,"fork":false,"pushed_at":"2024-11-28T18:02:03.000Z","size":20,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-01-28T21:33:36.282Z","etag":null,"topics":["functions","php","wordpress","wp","wp-posts"],"latest_commit_sha":null,"homepage":"","language":null,"has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"gpl-3.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/widilo.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-11-28T17:22:03.000Z","updated_at":"2024-11-28T18:02:06.000Z","dependencies_parsed_at":null,"dependency_job_id":"5e6741fc-7f8b-4685-984f-17a2ea7109b7","html_url":"https://github.com/widilo/auto-generate-wordpress-post-title","commit_stats":null,"previous_names":["widilo/auto-generate-wordpress-post-title"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/widilo%2Fauto-generate-wordpress-post-title","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/widilo%2Fauto-generate-wordpress-post-title/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/widilo%2Fauto-generate-wordpress-post-title/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/widilo%2Fauto-generate-wordpress-post-title/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/widilo","download_url":"https://codeload.github.com/widilo/auto-generate-wordpress-post-title/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245121357,"owners_count":20564112,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["functions","php","wordpress","wp","wp-posts"],"created_at":"2024-11-30T15:08:30.893Z","updated_at":"2025-10-04T07:10:09.360Z","avatar_url":"https://github.com/widilo.png","language":null,"funding_links":[],"categories":[],"sub_categories":[],"readme":"# Auto generate wordpress post title from first line\n\n### Overview:\n\nThe `auto_generate_title_from_first_line` function automatically generates a post title based on the **first line** of the post content when a post is saved or updated. The function works by extracting the text before the first line break (`\\n`), removing any HTML tags, and trimming spaces. It then sets the post title to this text, but only if the title is not already set.\n\n------\n\n### Function Signature:\n\n```php\nfunction auto_generate_title_from_first_line($post_id);\n```\n\n------\n\n### Parameters:\n\n- **`$post_id` (int)**: The ID of the post being saved or updated. WordPress automatically passes this parameter when the `save_post` action is triggered.\n\n------\n\n### Logic:\n\n1. **Check Post Type and Autosave:**\n   - The function first checks if the post is of type `post` (i.e., not a page or other custom post type).\n   - It also ensures that the function does not run during WordPress's autosave process by checking the `DOING_AUTOSAVE` constant.\n2. **Get Post Content:**\n   - The function retrieves the post content using `get_post_field('post_content', $post_id)`.\n3. **Extract First Line:**\n   - The function extracts the **first line** of the content by splitting the content at the first line break (new line) using `strtok($post_content, \"\\n\")`. This ensures that only the content before the first line break is selected.\n4. **Check for Empty Line:**\n   - The function checks if the extracted first line is empty. If it is, the function stops and does not update the post title.\n5. **Remove HTML Tags and Trim Whitespace:**\n   - The first line is passed through `wp_strip_all_tags()` to remove any HTML tags that may be present.\n   - The function then trims leading and trailing spaces using `trim()` to ensure the title is clean.\n6. **Check and Set Post Title:**\n   - The function retrieves the current post title using `get_post_field('post_title', $post_id)`.\n   - If the post title is empty (i.e., not set), it updates the post title to the cleaned first line using `wp_update_post()`.\n\n------\n\n### WordPress Hook:\n\nThis function is hooked into the `save_post` action, which means it runs every time a post is saved or updated.\n\n```php\nadd_action('save_post', 'auto_generate_title_from_first_line');\n```\n\n------\n\n### Use Case:\n\nThis function is ideal for automatically generating a title from the post content when the title is not manually provided. It ensures that a relevant, content-based title is set when a new post is created or updated.\n\n------\n\n### Example:\n\nConsider the following post content:\n\n```\nThis is the first line of the post content.\nThis is the second line of the content.\n```\n\n- The function extracts \"This is the first line of the post content.\" as the first line.\n- If the post title is empty, the function sets the post title to \"This is the first line of the post content.\"\n\n------\n\n### Notes:\n\n- The function **only** sets the title if the current title is empty. If the post already has a title, the title remains unchanged.\n- HTML tags are stripped from the first line before it is set as the title. This ensures the title is clean and without any HTML markup.\n- The function uses the first line of the post content, so it does not consider the full paragraph or other content.\n\n------\n\n### Error Handling:\n\n- If the first line is empty, no changes are made, and the title is not updated.\n- If the post content does not have a first line (i.e., when it's empty), the function does nothing.\n\n------\n\n### Here’s the full function code:\n\n```php\nfunction auto_generate_title_from_first_line($post_id) {\n    // Only for posts (post type) and not during the auto-saving process\n    if (get_post_type($post_id) != 'post' || defined('DOING_AUTOSAVE') \u0026\u0026 DOING_AUTOSAVE) {\n        return;\n    }\n\n    // Get the content of the post\n    $post_content = get_post_field('post_content', $post_id);\n    \n    // Extract the first line by splitting at the first line break (new line)\n    $first_line = strtok($post_content, \"\\n\");\n\n    // Check if the first line is empty\n    if (empty($first_line)) {\n        return;\n    }\n\n    // Remove HTML tags (if any) and trim any leading or trailing spaces\n    $first_line = wp_strip_all_tags(trim($first_line));\n\n    // Only change the title if it is not set or if it is empty\n    $post_title = get_post_field('post_title', $post_id);\n    if (empty($post_title)) {\n        // Set the title to the first line of the content\n        wp_update_post(array(\n            'ID' =\u003e $post_id,\n            'post_title' =\u003e $first_line\n        ));\n    }\n}\nadd_action('save_post', 'auto_generate_title_from_first_line');\n```\n\n------\n\n### Conclusion\n\nThe `auto_generate_title_from_first_line` function is a simple and effective way to automatically generate post titles based on the content. By using the first line of the content, this function ensures that titles are relevant to the body of the post, while also ensuring that HTML tags are stripped out and extra spaces are removed.\n\n### Changelog\nVersion 1.0\n\n    Initial release: Automatically generates titles based on the first line of the content.\n\n### Support\n\nIf you encounter any issues with the code or have suggestions for improvement, please contact the developer: hallo@widilo.de\n\n### License\n\nThis Code is released under the GNU General Public License (GPL v.3)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwidilo%2Fauto-generate-wordpress-post-title","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fwidilo%2Fauto-generate-wordpress-post-title","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwidilo%2Fauto-generate-wordpress-post-title/lists"}