add texonomy to wordpress post with javascript

I’ve developed a plugin that adds a button to the edit.php page in the WordPress admin. This button populates certain fields with data from a remote JSON file. However, I’m having trouble adding the title and taxonomy data correctly. Could someone please help me figure out how to do this?

Here is my code:

    <script>
    document.addEventListener('DOMContentLoaded', function() {
        document.getElementById('loadButton').addEventListener('click', function(event) {
            event.preventDefault();

            var imdbID = document.getElementById('imdb-title').value;
            var url = '/wp-content/plugins/imdb/get.php?id=' + imdbID;

            fetch(url)
                .then(response => response.json())
                .then(data => {
                    //document.getElementById('name').value = data.name || '';
                    document.getElementById('ratings-movie').value = data.score || '';
                    document.getElementById('year-type').value = data.year || '';
                    document.getElementById('period-time').value = data.duration || '';
                    document.getElementById('rating').value = data.vote || '';
                    document.getElementById('gross-worldwide-movie').value = data.boxoffice || '';  
                    document.getElementById('age-limit').value = data.contentrating || '';
                    document.getElementById('awards-series').value = data.awards || '';
                    document.getElementById('metascore').value = data.metascore  || ''; 
                    document.getElementById('top250-movies').value = data.imdbrate   || '';
                    document.getElementById('budget-movie').value = data.budget  || '';
                    //document.getElementById('title').value = data.name;
                    var tags = '1,2,3,4,5,56,7';
                    //var tags = data.tags || '';  
                    var tagInput = document.getElementById('components-form-token-input-0'); 
                    if (tags && tagInput) {
                    tags.split(',').forEach(function(tag) {
                        tag = tag.trim();
                        if (tag) {

                            tagInput.value = tag;

                            document.getElementById('submit_tag').click();
                        }
                    });
                    }
            })
                .catch(error => {
                    console.error('Error fetching data:', error);
                    alert('Error loading JSON data.');
                });
        });
    });
    </script>

the title and tags (taxonomy) fields are not being filled as expected. All other fields are populated correctly, but the title remains empty, and the tags are not assigned

6

It’s hard to tell precisely what the problem is without also seeing the HTML for this as well, however a few things stand out:

  • The title and name fields use the IDs “title” and “name” which are quite generic and I would not at all be surprised if there is also something else on the page with the same ID. If that’s the case when you use document.getElementById() it will only ever find the first element with that ID.
  • With regards to the tags, you’re clicking a button in a tight loop (i.e. you’re not waiting for a result before continuing), depending on the mechanism behind that tag submission, you may see unexpected behaviour. If it submits to the server it may be better to wait for the reponse to come back before attempting to submit the next tag, if it adds it to the UI and stores it locally before submitting, you may want to verify that the item was added in the UI where you expect to see it. I think regardless of which method is happening, using a “waitFor” type method to wait for the item to be present in the UI is probably a good approach (assuming that if it gets sent to the server it is added to the UI after a successful request).

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật