I am working on a poetry site and I am new to wordpress. I want to publish poems of various authors on my site. What i want is that when i publish a post, a link/onclick event on every word of post may be generated automatically in background. When a user click on any word of post, he may be directed to a new page containing poems with that similar word. Any help or direction will be appreciated a lot.
I don't know the scale of your project, my solution will help you to get around it quickly, but it's inefficient when it comes to larger scaled projects.
The easy way
You can add a filter to
the_content filter and finally link every word inside of the content to the search page of that specified word.
The Hard Way
This is way much better than the first solution when it comes to scale and efficiency.
By assuming that your content is in English and are divided using some character, the general process would be:
- Adding a filter to the
- Striping all link tags to prevent linking a word multiple times.
- Using the
explodefunction to get an array of words inside the poem.
- Add links to the words by using a simple
To generate your link you can follow one of these approches:
- Creating a custom post type and use a plugin like
posts2poststo connect your posts and finally generating the link using
Creating a custom table for keeping track of words and the connected posts.
For the second approach, you have to add a query var by adding a filter to
query_varsfilter, create a custom shortcode to display your data and finally to generate the links you have to use
add_query_varfunction and a hard-coded value of the page you're putting your shortcode.