0

I am using wordpress rest api and am getting encoded title strings from the server. I want to decode the string before I use it to replace the document.title.

Wordpress api

{
 "id": 698,
 "title": {
  "rendered": "Ludovico Einaudi – “Divenire”"
 },
}

actions.js

export default {
  updateDocTitle ({ state, commit }, { parts = [], sep = ' – ' }) {
    commit('SET_DOC_TITLE', parts.join(sep))
    document.title = state.site.docTitle
  },
}

Tried document.title = decodeURI(state.site.docTitle) - doesn't work

mutations.js

export default {
  SET_DOC_TITLE(state, title) {
    state.site.docTitle = title
  }
}

Tried state.site.docTitle = decodeURI(title) - doesn't work

Component

computed: {
    post() {
      return this.$store.getters.singleBySlug(this.request)
    }
  },
  methods: {
    getPost() {
      this.$store.dispatch('getSingleBySlug', this.request).then(() => {
        this.$store.dispatch('updateDocTitle', { parts: [ this.post.title.rendered, this.$store.state.site.name ] })
      })
    }
  },
  created() {
    this.getPost()
  }

Tried this.$store.dispatch('updateDocTitle', { parts: [ decodeURI(this.post.title.rendered), this.$store.state.site.name ] }) - doesn't work

shubhra
  • 772
  • 10
  • 26
  • What do you get where you say it doesn't work? Some kind of error? Garbled text? something else? – Matt Ellen Aug 29 '19 at 15:05
  • It shows ```Ludovico Einaudi – “Divenire”``` as it is. No errors. – shubhra Aug 29 '19 at 15:07
  • 2
    Those are HTML entities, have a look at this answer: https://stackoverflow.com/questions/5796718/html-entity-decode – Vidar Aug 29 '19 at 15:07
  • This https://stackoverflow.com/a/29157703/2303467 helped me in understand how decodeURI() works. Got my solution here - https://stackoverflow.com/questions/7394748/whats-the-right-way-to-decode-a-string-that-has-special-html-entities-in-it/7394787#7394787. @Vidar Thank you! – shubhra Aug 29 '19 at 15:31

0 Answers0